Servlets: Processing Form Parameters

Problem

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

Solution

Use the request object’s getParameter( ) method.

Discussion

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.

Random numbers HTML page

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.

Random numbers servlet output

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

<HTML> 
<HEAD><TITLE>Random Numbers Page</TITLE></HEAD> 
<BODY BGCOLOR="white"> 
<H1>Random Numbers Page</H1> 
<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) 
<BR> 
<INPUT TYPE="SUBMIT" VALUE="Get YOURS!"> 
</FORM> 
</BODY></HTML>

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 {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter(  );
    
        // The usual HTML setup stuff.
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<BODY BGCOLOR="white">");

        // HTML for this page
        out.println("<TITLE>Your Personal Random Numbers</TITLE>");
        out.println("<H1>Your Personal Random Numbers</H1>");
        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.");
        out.println("<OL>");

        // 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(num);
                out.println(" as a number.</P>");
            }
        }

        // Now actually generate some random numbers.
        Random r = new Random(  );
        for (int i=0; i<n; i++) {
            out.print("<LI>");
            out.println(r.nextInt(49));    // for Lotto 6/49
        }
        out.println("</OL>");

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

See Also

The online source includes OrderServlet, a slightly longer example.

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

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