ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Web Server Java -- Servlets and JSP
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

Servlets: Processing Form Parameters

You want to process the data from an HTML form in a servlet.

Use the request object's getParameter( ) method.


Each uniquely named INPUT element in the FORM on the HTML page makes an entry in the request object's list of parameters. These can be obtained as an enumeration, but more commonly you request just one. Figure 18-2 shows a simple form that asks you how many random numbers you want generated, and makes up that many for you.

Screen shot.
Figure 18-2. Random numbers HTML page

When I type the number 8 into the field and press the "Get Yours" button, I see the screen shot in Figure 18-3.

Screen shot.
Figure 18-3. Random numbers servlet output

How does it work? The program obviously consists of both an HTML page and a Java servlet. The HTML page appears in Example 18-3; notice the FORM entry and the INPUT field.

Example 18-3: IntsServlet.htm

<HEAD><TITLE>Random Numbers Page</TITLE></HEAD>
<BODY BGCOLOR="white">
<H1>Random Numbers Page</h2>
<P>This site will let you pick some random numbers for Lottery, lucky number or other purposes, all electronically.</P>
<FORM METHOD=POST ACTION="/servlets/IntsServlet">
<H4>How Many Numbers Do You Want Today?</H4>
<INPUT NAME=howmany SIZE=2> (default is 5)

Example 18-4 shows the Java for the servlet. Watch for the use of getParameter( ).

Example 18-4: IntsServlet.java

import java.io.*;
import java.util.Random;
import javax.servlet.*;
import javax.servlet.http.*;
public class IntsServlet extends HttpServlet {
  protected final int DEFAULT_NUMBER = 5;
  /** Called when the form is filled in by the user. */
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws IOException {
    PrintWriter out = resp.getWriter( );
    // The usual HTML setup stuff.
    out.println("<BODY BGCOLOR=\"white\">");
    // HTML for this page
    out.println("<TITLE>Your Personal Random Numbers</TITLE>");
    out.println("<H1>Your Personal Random Numbers</h2>");
    out.println("<P>Here are your personal random numbers,");
    out.println("carefully selected by a");
    out.println("<A HREF=\"http://java.sun.com\">Java</A> program.");
    // Figure out how many numbers to print.
    int n = DEFAULT_NUMBER;
    String num=req.getParameter("howmany");
    if (num != null && num.length( ) != 0) {
      try {
        n = Integer.parseInt(num);
      } catch (NumberFormatException e) {
        out.println("<P>I didn't think much of ");
        out.println(" as a number.</P>");
    // Now actually generate some random numbers.
    Random r = new Random( );
    for (int i=0; i<n; i++) {
      out.println(r.nextInt(49));  // for Lotto 6/49

    // Print a break and a back link.
    out.println("<A HREF=\"index.html\">Back to main Page</A>");

See Also: The online source includes OrderServlet, a slightly longer example.

Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

Next Pagearrow