Some Ancillary Issues

 

Introduction.  We will take a page to cover some ancillary issues associated with developing JSPs.
  • some more JSP tags
  • comments in JSP
  • declaring instance variables and methods

Some More JSP Tags.  The following table contains a brief survey of some JSP tags that we will use.  Some of these have already been used in the previous JSP.

 

Tag Pair Name Purpose
<%   %> JSP Scriptlet A block of Java statements
<%=   %> JSP Expression To display the string value of an expression within HTML
<%@   %> JSP Directive These statements set conditions that apply to the entire JSP
<%--   --%> JSP Comment Comment lines that will be ignored by the JSP engine
<%!   %> JSP Declaration Used to declare instance variables and methods for a JSP

 

In order to illustrate these at least to some extent we will work with another form and processing script pair.  But we will base these pages on the getParameterNames( ) method of the request class.

We will use a variation on our typical forms page that contains all the HTML input controls, but it also allows for multiple selections in the select control.  The form page should be called get_parameter_names.html.

 

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

<body bgcolor = "003344" text="cccccc">
<form action="handle_get_parameter_names.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" size=4 multiple>
<option value="Amex">American Express
<option value="DinersClub">Diner's Club
<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>

 

The form shouldn't look much different than usual.

 

 

The processing script follows and makes use of the above special JSP tags which will be explained after.  You should call the file handle_get_parameter_names.jsp.

 

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

<body bgcolor = "003344" text="cccccc">
<%@ page import = "java.util.Enumeration" %>
<%
// obtaining all parameter names on the form
// defining local variables
Enumeration names = request.getParameterNames( );
%>
<!-- Some HTML with embedded JSP to display the results -->
You passed the following parameters <P>
<% while (names.hasMoreElements( ))
{

String parameterName = (String) names.nextElement();
// get parameter values in case there are multiple values
String paramValues[] = request.getParameterValues(parameterName);

// if there is only one value print it out
if(paramValues.length == 1)
{

out.println(parameterName + " = " + paramValues[0] + "<br>");

}
else
{

out.println(parameterName + " = <br>");
for (int i=0; i < paramValues.length; i++)
{

out.println(paramValues[i] + "<br>");

}
out.println( );

}

}
%>
<!-- a jsp sort of comment -->
<%-- out.println("Print This!"); --%>
</body>
</html>

 

  • I have changed the colors on the Java comments to green.
  • I have changed the HTML comments to olive.
  • The JSP comment is in purple at the bottom of the JSP page.
  • The directive to import the util.Enumeration package is in maroon.
  • We will create instance variables and methods in later web pages.

The results of the processing are contained in the following image.

 

 

Notice how it goes through all of the parameters on the form without knowing their names.  You should also notice the code requires special knowledge of how to deal with Enumeration objects.