Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents

Trail:
Servlets
Lesson:
Overview of Servlets



Help on searching
is available.

Search technology
by Excite, Inc.

A Simple Servlet

The following class completely defines servlet:
 

    public class SimpleServlet extends HttpServlet
    { 
        /**
         * Handle the HTTP GET method by building a simple web page.
         */
        public void doGet (HttpServletRequest request,
                	   HttpServletResponse response)
        throws ServletException, IOException
        {
	    PrintWriter		out;
	    String		title = "Simple Servlet Output";

	    // set content type and other response header fields first
            response.setContentType("text/html");

	    // then write the data of the response
	    out = response.getWriter();

            out.println("<HTML><HEAD><TITLE>");
	    out.println(title);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<H1>" + title + "</H1>");
	    out.println("<P>This is output from SimpleServlet.");
	    out.println("</BODY></HTML>");
	    out.close();
        }
    }

That's it!

The classes mentioned in the Architecture of the Servlet Package section are shown in the example in bold:

  • SimpleServlet extends the HttpServlet class, which implements the Servlet interface.
     
  • SimpleServlet overrides the doGet method in the HttpServlet class. The doGet method is called when a client makes a GET request (the default HTTP request method), and results in the simple HTML page being returned to the client.
     
  • Within the doGet method,
     
    • The user's request is represented by an HttpServletRequest object.
       
    • The response to the user is represented by an HttpServletResponse object.
       
    • Because text data is returned to the client, the reply is sent using the Writer object obtained from the HttpServletResponse object.
       
 
Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents