HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Migrating CGI Scripts"
Vol. 3, Issue 1, p. 8

	

Listing 1.
 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 
public class TestWebServlet extends HttpServlet 
{ 

    public void service(HttpServletRequest request,  
HttpServletResponse response) 
        throws ServletException, IOException 
    { 
//  get input from the client using the HttpServletRequest parameter 

 //  process the data 

 //  return output to the client using the HttpServletResponse parameter 
    } 
     
} 

Listing 2.
 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 

//  File:  TestWebServlet.java 
//  Listing 1 
// 
/** 
 * 
 *  This servlet responds with a simple thank-you note displaying <p> 
 *  the client's IP address. 
 * 
 *  @author Chad (shod) Darby,  [email protected] 
 *  @version 1.369, 13 Nov 97 
 * 
 */ 
public class TestWebServlet extends HttpServlet 
{ 
    protected void service(HttpServletRequest request,  
HttpServletResponse response) 
        throws ServletException, IOException 
    { 
        String clientIPAddress = request.getRemoteAddr(); 
        String replyPage = null; 

        //  Build HTML page 
        // 
        replyPage = "<HTML><HEAD><TITLE>Test Web Servlet</TITLE></HEAD>"; 
        replyPage += "<BODY>"; 
        replyPage += "<P>Thanks for calling the servlet from " + clientIPAddress; 
        replyPage += "</BODY></HTML>"; 

        //  Set the content type of response 
        // 
        response.setContentType("text/html"); 

        //  Get the output stream to the client response and send the page. 
        //  Note:  The PrintWriter class is a new addition to Java 1.1 
        // 
        PrintWriter out = new  
PrintWriter(response.getOutputStream()); 
        out.println(replyPage); 
        out.flush(); 
        out.close(); 
    } 
} 

Listing 3.
 
import javax.servlet.*; 
import javax.servlet.http.*; 

import java.io.*; 
import java.util.*; 

//  File:  EchoEnvironmentServlet.java 
//  Listing 2 
// 
/** 
 *  This servlet displays the CGI environment variables in a web page. 
 * 
 *  @author Chad (shod) Darby,  [email protected] 
 *  @version 3.41, 2 Nov 1997 
 * 
 */ 
public class EchoEnvironmentServlet extends HttpServlet 
{ 

    public void service(HttpServletRequest request,  
HttpServletResponse response) 
     throws IOException 
    { 
        String replyPage = null; 

        // Build the header for HTML page 
        // 
        replyPage = "<HTML><HEAD><TITLE>Echo Environment  
Variables</TITLE></HEAD>"; 

        replyPage += "<BODY>"; 

        // Echo the environment variables 
        // 
        replyPage += "<H2>  Environment Variables </H2>"; 

        replyPage += "<p><b> Server Name: </b>" +  
request.getServerName(); 
        replyPage += "<p><b> Server Port: </b>" +  
request.getServerPort(); 
        replyPage += "<p><b> Server Protocol: </b>" +  
request.getProtocol(); 
        replyPage += "<p><b> Servlet Name: </b>" +  
request.getServletPath(); 

        replyPage += "<p><b> Method: </b>" + request.getMethod(); 
        replyPage += "<p><b> Path Info: </b>" +  
request.getPathInfo(); 
        replyPage += "<p><b> Path Translated: </b>" +  
request.getPathTranslated(); 
        replyPage += "<p><b> Query String: </b>" +  
request.getQueryString(); 
        replyPage += "<p><b> Request URI: </b>" +  
request.getRequestURI(); 

        replyPage += "<p><b> Remote Host: </b>" + request.getRemoteHost(); 
        replyPage += "<p><b> Remote Addr: </b>" + request.getRemoteAddr(); 
        replyPage += "<p><b> Remote User: </b>" +  
request.getRemoteUser(); 
        replyPage += "<p><b> Authorization Type: </b>" + request.getAuthType(); 

        replyPage += "<p><b> Content Type: </b>" +  
request.getContentType(); 
        replyPage += "<p><b> Content Length: </b>" +  
request.getContentLength(); 

        replyPage += "<hr>"; 

        // Just for kicks, let's loop thru and get the header info using an enumeration 
        // 
        replyPage += "<H2>  Request Header Information </H2>"; 
        Enumeration e = request.getHeaderNames(); 
        String name; 

        while (e.hasMoreElements()) 
        { 
          name = (String) e.nextElement(); 
          replyPage += "<p><b>" + name + ": </b>" +  
request.getHeader(name); 
        } 
        replyPage += "<hr>"; 

        //  Build bottom of HTML page 
        // 
        replyPage += "</BODY> </HTML>"; 

        //  Set the content-type for the response header 
        //  note: no need for extra carriage returns! 
        // 
        response.setContentType("text/html"); 

        //  finally, send this formatted HTML page back to the client 
        // 
        PrintStream out = new PrintStream(response.getOutputStream()); 
        out.println(replyPage); 
        out.close(); 
  } 

  public String getServletInfo() 
  { 
    return "Echo Form Servlet,  Chad (shod) Darby, 2 Nov 1997, www.j-nine.com"; 
  } 
} 

Listing 4.
 
<!-- FILE:  MagazineForm.html --> 
<!-- LISTING 3                --> 

<HTML> 
<HEAD> 
   <TITLE>Magazine Subscriptions</TITLE> 
</HEAD> 
<BODY> 

<CENTER> 
<H1> 
Magazine Subscription Form</H1></CENTER> 

<HR><FORM method="GET" action="http://127.0.0.1:8080/servlet/EchoMagazineForm"> 
<H3> 
Name and Address</H3> 

<TABLE> 
<TR> 
<TD>Name</TD> 

<TD><INPUT type=text size=20 name="Name"></TD> 
</TR> 

<TR> 
<TD>Address</TD> 

<TD><INPUT type=text size=20 name="Address"></TD> 
</TR> 

<TR> 
<TD>City</TD> 

<TD><INPUT type=text size=20 name="City"></TD> 
</TR> 

<TR> 
<TD>State</TD> 

<TD><INPUT type=text size=20 name="State"></TD> 
</TR> 

<TR> 
<TD>Zip Code</TD> 

<TD><INPUT type=text size=20 name="Zip Code"></TD> 
</TR> 
</TABLE> 

<H3> 
Payment Method</H3> 

<OL> 
<LI> 
Bill Me In Full  <INPUT type="radio" name="Payment Method" value="Bill Me In Full"></LI> 

<LI>  
Bill Me In 3 Installments  <INPUT type="radio" name="Payment Method" value="Bill Me In 3 Installments"></LI> 

<LI>  
Payment Enclosed  <INPUT type="radio" name="Payment Method" checked value="Payment Enclosed"></LI> 
</OL> 

<TABLE> 
<TR> 
<TD></TD> 

<TD><INPUT type=submit value="Subscribe"></TD> 

<TD><INPUT type=reset value="Reset"></TD> 
</TR> 
</TABLE> 
</FORM> 
<HR> 
</BODY> 
</HTML> 

Listing 5.
 
import javax.servlet.*; 
import javax.servlet.http.*; 

import java.io.*; 
import java.util.*; 

//  File:  EchoMagazineServlet.java 
//  Listing 4 
// 
/** 
 *  This servlet displays the fields of a magazine subscription form in a web page. 
 * 
 * 
 */ 
public class EchoMagazineServlet extends HttpServlet 
{ 
  private int counter; 

  public void init(ServletConfig config) throws ServletException 
  { 
    super.init(config); 
    counter = 0; 
  } 

  public void service(HttpServletRequest request,  
HttpServletResponse response) 
 throws ServletException, IOException 
  { 
    String replyPage = null; 

    // our servlet is being called, increment the counter 
    // 
    counter++; 

    //  Build HTML page’s header information 
    // 
    replyPage = "<HTML><HEAD><TITLE>Subscription Confirmation"; 
    replyPage += "</TITLE></HEAD>"; 

    replyPage += "<BODY>"; 
    replyPage += "<H1>Subscription Confirmation</H1>"; 
    replyPage += "<HR>"; 

    //  Thank user for subscribing and show value of "counter" 
    // 
    replyPage += "Thank you for subscribing."; 
    replyPage += "You are subscriber <b># " + counter + "</b>"; 
    replyPage += "<P>We will send the magazine to the mailing address below."; 

    //  Print out the values for form variables. 
    //  Notice we use HTML formatting to "spiff-up" the data 
    // 
    replyPage += "<UL>"; 
    replyPage += "<LI> <B>Name:</B>  " + request.getParameter("Name"); 
    replyPage += "<LI> <B>Address:</B>  " + request.getParameter("Address"); 
    replyPage += "<LI> <B>City:</B>  " + request.getParameter("City"); 
    replyPage += "<LI> <B>State:</B>  " + request.getParameter("State"); 
    replyPage += "<LI> <B>Zip Code:</B>  " + request.getParameter("Zip Code"); 
    replyPage += "<LI> <B>Payment Method:</B> " +  
request.getParameter("Payment Method"); 
    replyPage += "</UL>"; 

    replyPage += "<HR>"; 
         
    replyPage += "<P>" + this.getServletInfo(); 
     
    //  Build bottom of HTML page 
    // 
    replyPage += "</BODY></HTML>"; 

    //  Set the content-type for the response header 
    // 
    response.setContentType("text/html"); 

    //  finally, send this formatted HTML page back to the client 
    // 
    PrintWriter out = new PrintWriter(response.getOutputStream()); 
    out.println(replyPage); 
    out.close(); 
  } 

  public String getServletInfo() 
  { 
    return "Echo Magazine Form Servlet v5.49, 2 Nov 1997"; 
  } 
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.