Source of WelcomeServlet2.java


  1: // Fig. 26.12: WelcomeServlet2.java
  2: // Processing HTTP get requests containing data.
  3: import javax.servlet.ServletException;
  4: import javax.servlet.http.HttpServlet;
  5: import javax.servlet.http.HttpServletRequest;
  6: import javax.servlet.http.HttpServletResponse;
  7: import java.io.IOException;
  8: import java.io.PrintWriter;
  9: 
 10: public class WelcomeServlet2 extends HttpServlet 
 11: {   
 12:    // process "get" request from client
 13:    protected void doGet( HttpServletRequest request, 
 14:       HttpServletResponse response )
 15:          throws ServletException, IOException 
 16:    {
 17:       String firstName = request.getParameter( "firstname" );
 18:       
 19:       response.setContentType( "text/html" );
 20:       PrintWriter out = response.getWriter();  
 21:       
 22:       // send XHTML document to client
 23: 
 24:       // start XHTML document
 25:       out.println( "<?xml version = \"1.0\"?>" );
 26: 
 27:       out.printf( "%s%s%s", "<!DOCTYPE html PUBLIC", 
 28:          " \"-//W3C//DTD XHTML 1.0 Strict//EN\"",
 29:          " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" );
 30: 
 31:       out.println( "<html xmlns = \"http://www.w3.org/1999/xhtml\">" );
 32:       
 33:       // head section of document
 34:       out.println( "<head>" );
 35:       out.println( 
 36:          "<title>Processing get requests with data</title>" );
 37:       out.println( "</head>" );
 38:       
 39:       // body section of document
 40:       out.println( "<body>" );
 41:       out.println( "<h1>Hello " + firstName + ",<br />" );
 42:       out.println( "Welcome to Servlets!</h1>" );
 43:       out.println( "</body>" );
 44:       
 45:       // end XHTML document
 46:       out.println( "</html>" );
 47:       out.close();  // close stream to complete the page
 48:    } // end method doGet
 49: } // end class WelcomeServlet2
 50: 
 51: /**************************************************************************
 52:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 53:  * Pearson Education, Inc. All Rights Reserved.                           *
 54:  *                                                                        *
 55:  * DISCLAIMER: The authors and publisher of this book have used their     *
 56:  * best efforts in preparing the book. These efforts include the          *
 57:  * development, research, and testing of the theories and programs        *
 58:  * to determine their effectiveness. The authors and publisher make       *
 59:  * no warranty of any kind, expressed or implied, with regard to these    *
 60:  * programs or to the documentation contained in these books. The authors *
 61:  * and publisher shall not be liable in any event for incidental or       *
 62:  * consequential damages in connection with, or arising out of, the       *
 63:  * furnishing, performance, or use of these programs.                     *
 64:  *************************************************************************/
 65: