MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

Your first Web application

Now for a Web application that dynamically creates the simple Web page previously created using Java. As before, we begin by creating a subdirectory within webapps called “hello”.

Now we create the Java class which will handle the request for the Web page. In your favourite text editor, create a new file called HelloServlet.java and add the following text:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request,
		      HttpServletResponse response)
	throws ServletException, IOException
    {
	PrintWriter out = response.getWriter();

	out.println("<html>\n\t<head>\n\t\t<title>Hello!</title>\n\t</head>”);
	out.println("\t<body>\n\t\tHello, world.\n\t</body>”);
    }
}
    

The javax.servlet packages are distributed with Tomcat in the servlet-api.jar file. You will find this in the “lib” subdirectory of your Tomcat distribution. Ensure that this file is in your classpath when compiling this code. If you are compiling from the command line, you can do this with the following line:

javac -classpath "<path to Tomcat>\lib\servlet-api.jar" HelloServlet.java
    

Replace <path to Tomcat> with the Tomcat directory.

The first three lines import packages used in the code, and the contents of the two Servlet packages will be covered later. All of your Servlets will extend the HttpServlet class, which you will find in the javax.servlet package.

The class has one method, doGet(). This method is used to implement a GET request. This is appropriate, as when the Web browser requests the page produced by the Servlet it will be done using a GET request. doPost() is a similar method which takes the same arguments, but is called when a POST request is made. A Servlet may implement either or both methods, as it requires.

doGet() takes two arguments: an HttpServletRequest and HttpServletResponse. These encapsulate information concerning the request and the response the Servlet will make, in respect. This example is extremely simple, and only returns a message to the Web browser. It does this by getting the PrinterWriter instance provided by the response object. This is the exact same type of object as System.out, only it is used to return information to a Web browser.

Next, the class writes HTML output to the PrinterWriter instance. Notice that the output should produce exactly the same HTML as the previous example.

Compile the file. Inside the “hello” directory, create a directory called “Web-INF”, and inside this create a directory called “classes”. Copy the HelloServlet.class to this directory.

The Web application now needs to describe itself to Tomcat (or any other Servlet aware Web server it might run on) and also tell it what to do with the Servlet classes. This is done using the web.xml file. Create web.xml inside the Web-INF directory, and fill it with the following text:

<?xml version="1.0"?>
<!DOCTYPE Web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/Web-app_2_3.dtd">

<Web-app>
  <display-name>The Hello World Application</display-name>
  <description>Prints hello world!</description>

  <servlet>
    <servlet-name>hello</servlet-name>
    <description>This is a simple Servlet</description>

    <servlet-class>HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</Web-app>
    

The first two lines declare that this file is an XML file, and gives the files document type. The document type should always be the same. The Web-app element is used to describe the application as a whole. <display-name> and <description> describe and name the application to the Web server. Each Servlet provided by the Web application needs to be described in <servlet> elements. <servlet-name> must provide a name for the Servlet. This name does not have to be the same as the class name or the .class filename: it is the name that you will use throughout the rest of the web.xml when talking about the Servlet. <description> describes the Servlet in plain English. <servlet-class> names the actual Servlet class, in this case it is HelloServlet.

Each new Servlet must be described in its own <servlet> element, and each must have a unique name.

So far all that has been done is describe what Servlets are available to the Web application – now we need to tell it what to do with each Servlet. This is done with the <servlet-mapping> element. Each <servlet-mapping> element will map a particular Servlet (identified by its Servlet name) to a particular URL relative to the base Web application URL, as given by <url-pattern>. For instance, the above example of <url-pattern>/</url-pattern> says that any request made to the root address of the Web application should be handled by this Servlet. In this case, the URL would be http://localhost:8080/hello. On the other hand, if we had used <url-pattern>/greetings</url-pattern>, then the address of the Servlet would be http://localhost:8080/hello/greetings.

Once you have saved the web.xml file to Web-INF, and copied the .class file to the classes directory, restart Tomcat and visit http://localhost:8080/hello. You should see exactly the same message as the previous simple Web page produced. You can view the produced HTML by asking for the page source from your browser.

What has happened is that the web.xml has made Tomcat aware that all requests made to the root address of the Web application should be handled by the HelloWorld Servlet class. When your Web browser requests the address, it does so using the GET request method. Tomcat forwards this GET request to an instance of HelloServlet, which then prints out the HTML code returned to the Web browser.