Home arrow Java & J2EE arrow Page 9 - The JSP Files (part 4): The Red Pill

Beating It Into Submission - Java

Now that the basics are done with, it's time to begin applyingthat theory to Web development. This week, The JSP Files investigates theJSP Request object, and demonstrates how it can be used to process formdata. Learn about JSP's array variables, find out what chocolate chipcookies and couch potatoes have in common, and explore a very unusualpharmacy.

  1. The JSP Files (part 4): The Red Pill
  2. The Last Action Hero
  3. Entering The Matrix
  4. Requesting More
  5. Taking Some Medication
  6. What's For Dessert?
  7. A Chocolate Addiction
  8. Couch Potato
  9. Beating It Into Submission
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 6
March 07, 2001

print this article


You'll have noticed that in all the examples we've shown you thus far, we've used two pages - a single HTML page containing the form, and a separate JSP script which processes the form input and generates appropriate output. However, JSP provides an elegant method to combine those two pages into one via the form's SUBMIT button.

You've already seen that once a form is submitted to a JSP script, all the form variables become available to JSP. Now, in addition to the user-defined variables, each time you hit the SUBMIT button on a form, a variable named "submit" is created. And by testing for the presence or absence of this variable, a clever JSP developer can use a single JSP document to generate both the initial form and the output after it has been submitted.

The following code snippet demonstrates how the "welcome to The Matrix" example above could be rewritten using this technique.

<html> <head> <basefont face="Arial"> </head> <body> <center> <% // matrix.jsp // check for submit variable String submit = request.getParameter("submit"); if(submit != null) { // form has been submitted, display result // define the variables used in the scriptlet String fname; // assign values fname = request.getParameter("name"); // print the details out.println("Welcome to The Matrix, " + fname + "!"); } else { // display initial form %> <form method="GET" action="matrix.jsp"> <table cellspacing="5" cellpadding="5" border="0"> <tr> <td> <font size="-1">Name, rank and serial, number, soldier!</font> </td> <td align="left"> <input type="text" name="name" size="10"> </td> </tr> <tr> <td colspan="2" align="center"> <input name="submit" type="submit"> </td> </tr> </table> </form> <% } %> </center> </body> </html>
As you can see, the script first tests for the presence of the "submit" variable - if it doesn't find it, it assumes that the form has yet to be submitted and so displays the initial form.

Since the ACTION attribute of the
tag references the same JSP script, once the form has been submitted, the same script will be called to process the form input. This time, however, the "submit" variable will exist, and so JSP will not display the initial page, but rather the result page.

Note that for this to work, your

<input type="submit">
must have a NAME attribute with the value "submit", like this:

<input type="submit" name="submit">
And that's about it. Next time, we'll be hooking JSP up to a database in order to generate dynamic Web page - so don't miss that one. Until then...stay healthy!

>>> More Java & J2EE Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: