novocode.com
About Novocode.com  |  Contact  |  Site Map  |  Search

Need more information? Shop for books about Servlets at Amazon.com:

2. Servlets Step by Step

This chapter acts as a Servlet tutorial. You will learn how to use important techniques for Servlet development by writing some typical Servlets, ranging from very simple to rather complex. All examples in this chapter are fully functional and complete Servlets which have been successfully compiled and run.

2.1 Hello World!

This section shows how to

  • use the framework that makes up a simple Servlet
  • write a Servlet that provides static content (i.e. it produces the same output every time it is called by a client)

We start our venture into Servlet programming with the well-known "Hello World" example, this time named more suitably "Hello Client":

 1:  import java.io.*;
 2:  import javax.servlet.*;
 3:  import javax.servlet.http.*;
 4:
 5:  public class HelloClientServlet extends HttpServlet
 6:  {
 7:    protected void doGet(HttpServletRequest req,
 8:                         HttpServletResponse res)
 9:              throws ServletException, IOException
10:    {
11:      res.setContentType("text/html");
12:      PrintWriter out = res.getWriter();
13:      out.println("<HTML><HEAD><TITLE>Hello Client!</TITLE>"+
14:                  "</HEAD><BODY>Hello Client!</BODY></HTML>");
15:      out.close();
16:    }
17:
18:    public String getServletInfo()
19:    {
20:      return "HelloClientServlet 1.0 by Stefan Zeiger";
21:    }
22:  }

When you compile this Servlet and run it by requesting a URL which is assigned to it in a Web Browser it produces the following output:

A browser window showing the text "Hello Client!"

Let's have a look at how the Servlet works.

  • Lines 1 to 3 import some packages which contain many classes which are used by the Servlet (almost every Servlet needs classes from these packages).

     1:  import java.io.*;
     2:  import javax.servlet.*;
     3:  import javax.servlet.http.*;
    
  • The Servlet class is declared in line 5. Our Servlet extends javax.servlet.http.HttpServlet, the standard base class for HTTP Servlets.

     5:  public class HelloClientServlet extends HttpServlet
    
  • In lines 7 through 16 HttpServlet's doGet method is getting overridden.

     7:    protected void doGet(HttpServletRequest req,
     8:                         HttpServletResponse res)
     9:              throws ServletException, IOException
    10:    {
             ...
    16:    }
    
  • In line 11 we use a method of the HttpServletResponse object to set the content type of the response that we are going to send. All response headers must be set before a PrintWriter or ServletOutputStream is requested to write body data to the response.

    11:      res.setContentType("text/html");
    
  • In line 12 we request a PrintWriter object to write text to the response message.

    12:      PrintWriter out = res.getWriter();
    
    [API 2.0] ServletResponse.getWriter() is a new feature of JSDK version 2.0. If your Servlet engine does not support JSDK 2.0 you can replace the above line by "ServletOutputStream out = res.getOutputStream();". This change can be made in most of the example Servlets in this tutorial. The advantages of using ServletResponse.getWriter() are discussed in section 4.4.
  • In lines 13 and 14 we use the PrintWriter to write the text of type text/html (as specified through the content type).

    13:      out.println("<HTML><HEAD><TITLE>Hello Client!</TITLE>"+
    14:                  "</HEAD><BODY>Hello Client!</BODY></HTML>");
    
  • The PrintWriter gets closed in line 15 when we are finished writing to it.

    15:      out.close();
    

    This line is included for completeness. It is not strictly necessary. The Web Server closes the PrintWriter or ServletOutputStream automatically when a service call returns. An explicit call to close() is useful when you want to do some post-processing after the response to the client has been fully written. Calling close() tells the Web Server that the response is finished and the connection to the client may be closed as well.

  • In lines 18 through 21 we override the getServletInfo() method which is supposed to return information about the Servlet, e.g. the Servlet name, version, author and copyright notice. This is not required for the function of the HelloClientServlet but can provide valuable information to the user of a Servlet who sees the returned text in the administration tool of the Web Server.

    18:    public String getServletInfo()
    19:    {
    20:      return "HelloClientServlet 1.0 by Stefan Zeiger";
    21:    }