MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

Handling user input

In this example we will create a Web application which asks the user for their name, and then greets them. The greeting occurs on a separate page. Note that it is possible to do this all with JavaScript and the DOM in one page, on the browser. It is also possible to do this using only one page in a Web application, which we will do in the following chapter. For simplicity, however, we will use two Web pages.

As previously noted, GET and POST requests can both send data to the Web server, although both do so differently. Specifically, a GET request will encode the information in the actual URL, while a POST request hides the information in the HTTP header itself. Users will always able to see any information sent to the server using a GET request in the URL.

Because the two methods send data differently, the data must also be obtained from each request in a different way. For our convenience, the Servlet API hides all of this detail from us: all of the data is made available through the HttpServletRequest object, no matter which request method has been used, and no matter how the data was sent to the server.

We begin by creating a new Web application: create a directory in the Tomcat webapps subdirectory called “greetings”. Now we create a simple page that asks for the users name:

      <html>
	<head>
	  <title>Some Greetings</title>
	</head>
	<body>
	  What is your name?
	  <form action="response" method="GET">
	    <input type="text" name="user_name">
	    <input type="submit">
	  </form>
	</body>
      </html>
    

We save this as index.html. Of interest is the form. Notice that it sets its method to GET, which specifies how the information in the form is communicated to the server. It also is a hint that none of the Web application's internal state will change, otherwise POST would have been used. The form's action has also been set to “response”, which is the URL which the form's information will be sent to when the submit button is pressed. The form itself contains a text entry for the user to enter their name. The text entry has been called “user_name”. The Servlet will use this name when requesting the value in the text box.

You can view this page by opening the http://localhost:8080/greetings in your Web browser. “greetings” is just the directory name which you created in the webapps directory. Clicking the submit button should result in a page not found error.

We will now create the Servlet providing the response page:

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

public class ResponseServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request,
		      HttpServletResponse response)
	throws IOException
    {
	String name = request.getParameter("user_name");
	PrintWriter out = response.getWriter();
	
	out.println("<html><title>Hello! " + name + "</title</head>");
	out.println("<body>\n<h1>Hello, " + name + ".</h1>");
	out.println("It's good to meet you.");
    }
}
    

And here is a description of it, and the Web application, in the web.xml file.

<?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>A Greetings Application</display-name> 
  <description>Asks the user's name and then greets them.</description> 

  <servlet> 
    <servlet-name>ResponseServlet</servlet-name> 
    <description>Performs the actual greeting.</description> 

    <servlet-class>ResponseServlet</servlet-class> 
  </servlet> 

  <servlet-mapping> 
    <servlet-name>ResponseServlet</servlet-name> 
    <url-pattern>/response</url-pattern> 
  </servlet-mapping> 
</Web-app>
    

This Servlet uses the getParameter() method to request the value associated with the “user_name” entry in the form. getParameter() always returns a string, unless there is no entry in the form with the name passed as an argument to getParameter() – in this case it returns null.

Restart Tomcat. When you now open the Web application in your browser, you can enter your name and click the submit button. The application will now take you to a page greeting you by name.

This Servlet does currently have one obvious bug: it does not notice when no name has been entered, but the submit button has been pressed. Try this to see what happens. This problem can be fixed in two ways: first, JavaScript could be used in the browser to test if a name has actually been given. Secondly, the Servlet can itself test to see if a name has been given or not. We will implement the second method by modifying the Servlet's code to the following:

if (name.equals("")) {
    out.println("<html><title>Please enter your name</title</head>");
    out.println("<body>\n<h1>Oh no!</h1>");
    out.println("You have not entered your name. Please press the back button on your browser and enter your name.");
	}
else {
    out.println("<html><title>Hello! " + name + "</title</head>");
    out.println("<body>\n<h1>Hello, " + name + ".</h1>");
    out.println("It's good to meet you.");
}
    

Recompile the class and copy it into the classes subdirectory. Restart Tomcat and retest the Web application's behaviour when not entering a name.

This has just been a simple example of a Web application. Later, we will build a Servlet which will store state not only on the server, but also on the browser.