Source of WelcomeServlet.java


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