Simple Servlet Example

In Listing 12.5, you will generate a complete HTML page that displays a simple text string. This servlet extends the javax.servlet.http.HttpServlet class and overrides the HttpServlet.doGet() method.

Listing 12.5. HTMLPage Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HTMLPage extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res) 
  throws IOException {
    res.setContentType ("text/html");  // the content's MIME type
    PrintWriter out = res.getWriter();  // access the output stream
    out.println ("<HTML>");
    out.println ("<HEAD><TITLE>First Servlet</TITLE></HEAD>");
    out.println ("<BODY>");
    out.println ("<H1>My First Servlet Generated HTML Page</H1>");
    out.println ("</BODY>");
    out.println ("</HTML>");
  }
}

When the client browser sends the GET request for this servlet, the server invokes your doGet() method, passing it the HTTP request in the HttpServletRequest object. The response is sent back to the client in the HttpServletResponse object.

The HttpServletRequest interface provides access to information about the request, and its main use is to give access to parameters passed in the URL query string. For this simple example, the request object does not contain any useful information, so it is not accessed in this servlet. You will see how to use the HttpServletRequest object in later examples.

The HttpServletResponse object is used to return data to the client. For this simple example, the data content type is sent as the MIME type text/html. The PrintWriter object encodes the data that is sent to the client.

NOTE

A call to the setContentType() method should always be made before obtaining a PrintWriter object.


The out.println statements in Listing 12.5 generate the HTML that the browser will use to display the page.

In order to deploy your simple servlet, you must create a Web Application containing the servlet and store this in a Web Archive (WAR) file ready for deployment to a J2EE server. The following description takes you through the process of creating a Web Application using the deploytool provided with the J2EE RI.

If you do not want to create the Web Application, you can use deploytool to open the example supplied on the accompanying Web site as Day12/examples/j2ee-ri/examples.war and study the HTMLPage Web Component.

After compiling the code, start up the J2EE RI and run deploytool. You will now perform the following steps to deploy the servlet.

1.
From the File menu, select New, Web Component. This will bring up the New Web Application Wizard. The first page of the wizard displays simple help information that can be suppressed by the checking the box at the bottom of the page. Click on Next to select the WAR File page.

2.
On the WAR File page, select Create New Stand-Alone WAR Module in the WAR Location section. Under WAR Naming, select a location and filename for the WAR file. The location can be anywhere, but the code on the accompanying Web site uses a separate directory called j2ee-ri for storing EAR and WAR files but you must call the WAR file examples.war. Fill in examples as the WAR Name and set the Context root (Sun-specific Setting) to examples.

CAUTION

It is important that you keep the filename of the WAR file and the Web application context root consistent. If you deploy the application using the asadmin command (or the asant deploy-examples-j2ee-ri command), then the J2EE RI uses the name of the WAR file as the context root rather than the value supplied in the deployment descriptor. As long as you keep the WAR filename and context root consistent you will be fine. In this example the context root will be set to examples and the WAR filename toexamples.war.

Click the Edit button and add the HTMLPage class file from the Day12/examples/classes directory; deploytool will put the class file in a WEB-INF directory (why is explained later) as shown in Figure 12.5.

Figure 12.5. deploytool adding the class file.


3.
On the next page, choose Servlet as the Web component type, as shown in Figure 12.6.

Figure 12.6. deploytool Choose Component Type page.


4.
On the next page, select HTMLPage as the Servlet Class (see Figure 12.7). Accept the defaults for the remaining fields on this screen.

Figure 12.7. deploytool Component General Properties page.


5.
The next page of the wizard simply reminds you of the common steps needed to complete your Web Component. Select Finish after viewing this page.

6.
You will now add a component alias for your servlet. Click on your HTMLPage Web component in the left panel of deploytool. Select the Aliases tab in the right panel. Click on Add. Type /htmlpage, as shown in Figure 12.8.

Figure 12.8. deploytool Aliases page.


NOTE

The context root and component alias are used in the URL to locate your servlet as follows:

http://<Web server address>/<Context root>/<Component alias>

For this example you will browse to http://localhost:8000/examples/htmlpage

7.
As always, select the Tools, Verify J2EE Compliance menu to check the application; you will need to select the examples WAR file in the left hand pane to enable the Verify menu option. If all is OK, select Tools, Deploy to deploy your Web Application. You will be prompted to provide the administrative username and password before the deployment can proceed.

NOTE

If you are using the asant build files supplied with the case study you can use the command

asant deploy-examples-j2ee-ri

to deploy the WAR file you have created in the j2ee-ri sub-directory. Alternatively you could use the command

asant build deploy

to create and deploy the WAR file from source files. This creates theexamples.war file in the build sub-directory.


Now, start up a Web browser. First, type in the URL for the J2EE Web server (http://localhost:8000) and check that the server is up and running (see Figure 12.09).

NOTE

If you installed the J2EE RI with a Web server (HTTP) port other than 8000, then substitute your HTTP port number in this and all other browser URLs. For example, if you configured 1024 as the HTTP port number, then use the URL http://localhost:1024.


Figure 12.9. J2EE 1.4 server page.


Now access your servlet using the following URL (see Figure 12.10):

http://localhost:8000/examples/htmlpage

Figure 12.10. HTMLPage servlet.


Congratulations, you have just written and deployed your first servlet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.166.246