Processing requests

Servlet requests can come in a variety of ways. Your servlets are responsible for accepting those requests, parsing client input, and then generating a response. The methods of passing data to a servlet include:

To determine which method of request processing you can use, you must first understand the differences between the request methods, as defined by the HTTP specification.

GET vs POST

When a client submits an HTTP GET request to JRun, JRun invokes the doGet method of the servlet (if you overrode that method). The web browser sends an HTTP GET request when the user types a URL, clicks a link, or submits a form that specifies method=GET. The GET method is the most common method for invoking requesting a page. Any request parameters are stored in the request method header of the GET request message. This imposes limitations on the size and structure of the GET method.

When a client submits an HTTP POST request to JRun, JRun invokes the doPost method. The web browser sends an HTTP POST request when the user submits a form that specifies method=POST. The body of a POST request message stores request parameters. This lets the POST method contain more information than the GET method.

Using query string parameters

A query string passed from the client to the server in the HTTP request. It consists of name/value pairs (such as color=red). It can be part of the URL or appended to the HTTP request. The following URL shows the query string parameter color set to red and the query string parameter name set to danger:

http://localhost:8100/servlet/myservlet/?name=danger&color=red

Most modern web servers limit the length of a query string to 4K (4 000 characters). However, this can cause performance problems. If your query strings are becoming too unwieldy, use the POST method to send data from the client to the server. This is available using the FORM tag.

URL rewriting uses query string parameters to maintain session data across multiple requests. For more information on URL rewriting, see "Using URL rewriting".

Accessing query strings

You access a query string parameter using the request object's getParameter method, as the following example shows:

String name = request.getParameter("param-name");

The following line shows the signature of the getParameter method:

public abstract String getParameter(String name)

For example, if you request the following URL:

http://localhost:8100/servlet/myservlet/?name=danger&color=red

you can use the following code to extract the values of name and color:

String bgcolor = request.getParameter("color");
String first_name = request.getParameter("name");

You can iterate over all the query string parameters using the getParameterNames method, as the following example shows:

...
Enumeration eParmNames = req.getParameterNames();
while (eParmNames.hasMoreElements()) {
  String parm = (String) eParmNames.nextElement();
  out.println(" " + parm + " =  " + req.getParameter(parm) + "<br>");
}
...

To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.

Working with arrays

Parameters can have multiple values. Use the getParameterValues method to get an array of all values of the requested parameter, as the following example shows:

...
String[] state = req.getParameterValues("state");
for (int i = 0; i<state.length; i++) {
  out.println("<BR>You have a house in: " + state[i]);
}
...

Casting query strings

Query string parameters are Strings, but you can cast the results of the getParameter method to whatever you like. For example, to get the userID as an int, you use a line like the following:

int id = (int)(Integer.parseInt(req.getParameter("id")));

You set query string parameters in the response object by building URLs with them attached.

Using form input

Use the HTML FORM tag to create forms that users can fill out and submit. Your servlets commonly process this kind of data in the doPost method, since FORM methods are usually set to POST.

The following example shows an HTML form that prompts for user information:

...
out.println("<html><head><title>Sample Form</title>");
out.println("</head><body>");
out.println("<h1>Sample Form</h1>");
out.println("<FORM METHOD=\"POST\" ACTION=\"SampleForm\">");
out.println("First Name: ");
out.println("<INPUT TYPE=\"text\" NAME=\"firstname\" SIZE=40>");
out.println("<BR>Last Name: ");
out.println("<INPUT TYPE=\"text\" NAME=\"lastname\" SIZE=40>");
out.println("<INPUT TYPE=\"Submit\" VALUE=\"Submit\"></FORM>");
out.println("</body></html>");
...

You can then use request.getParameter methods to extract parameters from the POST message, as the following lines show:

...
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
...
String fname = req.getParameter("firstname");
String lname = req.getParameter("lastname");
...

Using the results of the getParameter methods, you can build logic into your servlet that determines its output, as the following lines show:

...
if (fname != null) {
  out.println("Hello " + fname + ", " + lname);
} else {
  out.println("Please fill out the following form:");
}
...

This sample posts the form to itself and tests for the presence of the request parameters. If the parameters are not null, the servlet prints a welcome message to the user; if they are null, the servlet prompts the user to enter a first and last name.

To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.

For information on processing different character sets in form submissions, see "Processing foreign-language form submissions".