[ Team LiB ] Previous Section Next Section

20.2 A Hello World Servlet

Example 20-1 is a listing of HelloNet.java, which implements a simple "Hello world" servlet, illustrated in Figure 20-1. The HelloNet servlet inherits from javax.servlet.http.HttpServlet and overrides the doGet( ) method to provide output in response to an HTTP GET request. It also overrides the doPost( ) method so it can respond to POST requests in the same way. The doGet( ) method outputs a string of HTML text. By default, this string is "Hello World".

Figure 20-1. The output of the HelloNet servlet
figs/Jex3_2001.gif

If the HelloNet servlet can determine a username, however, it greets the user by name. The servlet looks for a username in two places, starting in the HTTP request (an HttpServletRequest object), as the value of a parameter named username. If the servlet cannot find a request parameter with this name, it looks for an HttpSession object associated with the request and sees if that object has an attribute named username. Servlet-enabled web servers (i.e., servlet containers) provide a session-tracking layer on top of the stateless HTTP protocol. The HttpSession object allows a servlet to set and query named attributes with a single client session. Later in the chapter, we'll see an example that takes advantage of the session-tracking ability of this HelloNet servlet.

Example 20-1. HelloNet.java
package je3.servlet;
import javax.servlet.*;         // Basic servlet classes and interfaces
import javax.servlet.http.*;    // HTTP specific servlet stuff
import java.io.*;               // Servlets do IO and throw IOExceptions

/**
 * This simple servlet greets the user.  It looks in the request and session
 * objects in an attempt to greet the user by name.
 **/
public class HelloNet extends HttpServlet {
    // This method is invoked when the servlet is the subject of an HTTP GET
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException
    {
        // See if the username is specified in the request
        String name = request.getParameter("username");

        // If not, look in the session object.  The web server or servlet
        // container performs session tracking automatically for the servlet,
        // and associates an HttpSession object with each session.
        if (name == null) 
            name = (String)request.getSession( ).getAttribute("username");

        // If the username is not found in either place, use a default name.
        if (name == null) name = "World";

        // Specify the type of output we produce.  If this servlet is
        // included from within another servlet or JSP page, this setting
        // will be ignored.
        response.setContentType("text/html");

        // Get a stream that we can write the output to.
        PrintWriter out = response.getWriter( );

        // And, finally, do our output.
        out.println("Hello " + name + "!");
    }

    // This method is invoked when the servlet is the subject of an HTTP POST.
    // It calls the doGet( ) method so that this servlet works correctly
    // with either type of request.
    public void doPost(HttpServletRequest request,HttpServletResponse response)
        throws IOException
    {
        doGet(request, response);
    }
}

20.2.1 Running the HelloNet Servlet

Before you can run this servlet, you must compile and deploy it as described at the beginning of this chapter. To run a servlet, issue a request for it with a web browser. The URL you use depends on where the web server is running and how you deployed the servlet. If you are running the servlet container on your local machine, if the web server is listening on port 8080, and if you deploy the servlet as part of a web application named je3, using the web.xml file shown later in this chapter, you can run the servlet by pointing your browser at:

http://localhost:8080/je3/HelloNet

This should display a web page that reads "Hello World". For slightly more sophisticated output, provide a request parameter with a URL like the following (which was used to produce the output in Figure 20-1):

http://localhost:8080/je3/HelloNet?username=David
    [ Team LiB ] Previous Section Next Section