Processing Forms in JSP

 

Introduction.  While the following example is quite artificial I am working it this way to make certain we have a script that makes use of all of these form controls.

First you need to develop the HTML form page all_form.html.  This page has an example of at least one of each of the controls listed above.

 

<html>
<head>
<title>HTML Form</title>
</head>

<body bgcolor = "003344" text="cccccc">
<form action="handle_all_form.jsp" method=post>
<table>
<tr>
<td><font size = 4 color=cccccc>First Name:</font>
</td>
<td><input type=text name="txt_first_name" size=20>
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Last Name:</font>
</td>
<td><input type=text name="txt_last_name" size=20>
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>EMail Address:</font>
</td>
<td><input type=text name="txt_email" size=50>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4>Comments:</font>
</td>
<td><textarea name="tarea_comments" rows = 5 columns = 20></textarea>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Interests:</font>
</td>
<td><input type=checkbox name="chk_php">
<font size = 4 color=cccccc>PHP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_jsp">
<font size = 4 color=cccccc>JSP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_mysql">
<font size = 4 color=cccccc>MySQL</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Credit Card:</font>
</td>
<td><select name="sel_credit_card">
<option value="Discover">Discover
<option value="MasterCard">Mastercard
<option value="Visa">Visa
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Education:</font>
</td>
<td><input type=radio name="rb_education" value="NoCollege">
<font size = 4 color=cccccc>No College</font><br>
<input type=radio name="rb_education" value="College">
<font size = 4 color=cccccc>College</font><br>
<input type=radio name="rb_education" value="Masters">
<font size = 4 color=cccccc>Masters</font><br>
<input type=radio name="rb_education" value="PhD">
<font size = 4 color=cccccc>Ph.D.</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td colspan = 2 align = center><input type = submit name="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>

 

After uploading and running this in your account on the server you should see something like the following when you go to the page.

 

 

After entering information you want to process it.  This will be done with the following script which you should call handle_all_form.jsp.  Notice how this is the name of the action in the <form> tag.

 

<html>
<head>
<title>Displaying the Inputted Information</title>
</head>

<body bgcolor = "003344" text="cccccc">
<%
// obtaining the information from the form and displaying it
// defining local variables

String first_name = request.getParameter("txt_first_name");
String last_name = request.getParameter("txt_last_name");
String email = request.getParameter("txt_email");
String comments = request.getParameter("tarea_comments");
String php_interest = request.getParameter("chk_php");
String jsp_interest = request.getParameter("chk_jsp");
String mysql_interest = request.getParameter("chk_mysql");
String credit_card = request.getParameter("sel_credit_card");
String education = request.getParameter("rb_education");
%>

<!-- Some HTML with embedded JSP to display the results -->
Your first name is <b><%= first_name %> </b><BR>
Your last name is <b><%= last_name %> </b><BR>
Your email address is <b><%= email %> </b><BR>
Your comments are <b><%= comments %> </b><BR>
Your interest in PHP is <b><%= php_interest %> </b><BR>
Your interest in JSP is <b><%= jsp_interest %> </b><BR>
Your interest in MySQL is <b><%= mysql_interest %> </b><BR>
Your credit card is <b><%= credit_card %> </b><BR>
Your education level is <b><%= education %> </b><BR>

</body>
</html>

 

This is almost the simplest possible processing script.

But you should also notice how both the select/combo box and the radio button grouping have multiple options.

While it depends on what the user enters you should notice how the processing results correspond to the inputs.  This is illustrated in the following image.