Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Beating It Into Submission

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 <FORM> 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!

Note: All examples in this article have been tested on Linux/i586 with Tomcat 3.2 and JServ 1.1. Examples are illustrative only, and are not meant for a production environment. YMMV!
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links