[ Team LiB ] Previous Section Next Section

Different Ways to Do Forms

As with most flexible programming tools, you can arrange your JSP form handling code in a number of ways. Three of the most common arrangements of forms and their handlers are

  • A static HTML input form calling a JSP form handler

  • A JSP input form calling a separate JSP form handler

  • A single JSP page that displays the form and handles the input

Adding to the Mix—Using JSP and Servlets Together


You can organize your JSPs in many other ways, especially when you mix servlets with JSPs. Hour 20, "Building Web Applications with JavaServer Pages and Servlets," provides several other examples.

The HTML page shown way back in Listing 3.1 and the JSP in Listing 3.2 are examples of a static HTML form calling a JSP form handler. This form of Web programming has been the most popular for years—dating back to the origin of CGI programs. Many developers prefer to keep their Web sites as pure HTML, using JSP, servlets, or other server-side technologies only for handling form data.

Using a JSP input form isn't much different than a static HTML input form. However the possibilities are greatly expanded by the dynamic nature of JSPs. For example, instead of having fixed text or fields, you could read in a file or access a database to define the text or fields. Remember that, using JSPs, you have all of the facilities of the Java platform at your disposal.

The last commonly used arrangement is a little trickier, so we'll spend a moment discussing it.

Using the Same JSP for the Input Form and Form Handler

You might think it's a little strange, but putting the input form and the form handler in the same JSP page is sometimes a good idea. Typically, you combine the input form and form handler when the user needs to use the form multiple times, possibly seeing the results of the previous form submission.

For example, you might have a page for entering an order where you display the current order and allow the user to enter new items for the order. At the beginning of the page you handle the new order items entered by the user and then display the current order. At the end of the page you display the form to add new entries.

The first time the user hits the page, you can detect that there is no form data to process and just present the initial input form. After that, when you detect the presence of previous form data you can process it. There are a few different techniques you can use to detect the form data:

  • Look for the presence of form variables.

  • Pass a special variable indicating that this is a form submission.

  • Submit the form using the HTTP POST method and examine the request method in your form.

Looking for the form variables is the easiest of these methods. You may occasionally find times when it doesn't suit you, though. For example, you may pass in a set of form variables to the input form the first time it is displayed. The seed values might be embedded in the URL and passed from the Web page that led the user to the current input form. You wouldn't be able to tell the difference between the initial seed values and a form submission. Even so, this method should work for you most of the time.

In the case where you pass initial values to the input form, you can create a hidden variable on the form with an additional parameter, like "isSubmitted". The declaration in HTML would look like this:

<input type="hidden" name="isSubmitted" value="yes">

When the user accesses the form for the first time, you don't see this variable because the user hasn't submitted the input form. You can check to see whether request.getParameter("isSubmitted") returns null or not. If it returns null, the form was not submitted.

Sometimes you don't even want to go through the hassle of testing to see whether the form variables are there. You want to know right away whether the form was submitted. If a hyperlink is used to access the initial form (that is, the browser accesses the form with an HTTP GET) and you declare the method for the <form> tag to be POST, you can use request.getMethod() to determine whether the user actually submitted the form.

In other words, you set up your form like this:

<form action="someaction.jsp" method="POST">

You then use request.getMethod to see whether the form was submitted:

if (request.getMethod().equals("POST"))
    [ Team LiB ] Previous Section Next Section