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

"To Serve or not to Serve"
Vol. 3, Issue 1, p. 40


Listing 1: Hello World.
import javax.servlet.*; 
import java.io.*; 
public class HelloWorldServlet extends GenericServlet 
 public void init(ServletConfig _Config) 
   super( _Config ); 

 public void service ( ServletRequest _Req, ServletResponse _Res )  
        throws IOExeception 
  _Res.setContentType( "text/html" ); 

  PrintStream Output = new PrintStream( _Res.getOutputStream() ); 

  Output.println( "<HTML> <HEAD> <TITLE>Hello World</TITLE> </HEAD>"); 
  Output.println( "<BODY>" ); 
  Output.println( "<H1> HELLO WORLD!!!! </H1>" ); 
  Output.println( "</BODY> </HTML>" ); 

Listing 2: form2email.java.
import java.io.*; 
import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import sun.net.smtp.SmtpClient; 

public class form2email extends HttpServlet 
  public void service(HttpServletRequest _req, HttpServletResponse _res)  
         throws IOException 
    PrintStream   OutMail; 
    SmtpClient    sendmail; 

      //--- Setup the outgoing mail 
      String TO       = _req.getParameter("[email protected]"); 
      String FROM     = _req.getParameter("[email protected]"); 
      String SUBJECT  = _req.getParameter("Web Form"); 

      sendmail = new SmtpClient("mail.somewhere.com"); 
      sendmail.from( FROM ); 
      sendmail.to( TO ); 

      OutMail = sendmail.startMessage(); 

      OutMail.println("From: " + FROM ); 
      OutMail.println("To: " + TO ); 
      OutMail.println("Subject: " + SUBJECT ); 

      OutMail.println( "\n*********************\n" ); 

      //--- Retrieve all the HTML fields from the input stream 
      Enumeration keys; 
      String key; 
      String value; 

      keys = _req.getParameterNames(); 
      while (keys.hasMoreElements()) 
        key = (String) keys.nextElement(); 
        value = _req.getParameter(key); 
        OutMail.println( key + " = " + value); 

      //--- Complete the mail, and send it 
      OutMail.println( "\n*********************\n" ); 

    catch ( IOException E ){} 
    //--- Display thank you page to the client 
    PrintWriter Out = new PrintWriter( _res.getOutputStream() ); 

    Out.println( "<HTML><BODY>" ); 
    Out.println( "<B><I>Thank you</I></B>" ); 
    Out.println( "<BR><BR><B>...for taking the time to fill out this form</B>" ); 
    Out.println( "<BR><BR><B>A response has been sent.</B>" ); 
    Out.println( "</BODY></HTML>" ); 


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.