The aim of this workshop is to analyse the data entered into an HTML Form. Most of the work that you will do in JSP is this kind of form processing so it is important that you understand how each control works and in what format it sends it's data to the server.
This workshop should take no longer than 60 minutes.
The following demonstrates the objective of this workshop. First you are displayed an HTML page containg a form. enter whatever data you like and hit submit. The JSP that the data is posted to displays this data.
<HTML>
<HEAD>
<TITLE>JSP Course - Request Workshop</TITLE>
</HEAD>
<BODY>
<P>Your first name is <B><%= strFirstName %></B>
and your last name is <B><%= strLastName %></B></P>
<P>Your nationality is
<B><%= strNationality %></B></P>
<P>Your age group is <B><%= strAgeGroup %></B></P>
<P>Your favourite ice cream flavours are
<B><%= strFlavours %></B></P>
<P>At the weekend your like to do the following</P>
</BODY>
</HTML>
In the JSP above we can see several JSP expressions and we want to add code to this JSP that will provide values to the variables used in these JSP expressions.
String strFirstName =
request.getParameter("FirstName");
String strLastName =
request.getParameter("LastName");
if((strFirstName==null)||(strFirstName.equals("")))
strFirstName = "not specified";
if((strLastName==null)||(strLastName.equals("")))
strLastName = "not specified";
We will repeat this standard code of get form data then validate form data for each of the remaining elements.
String strNationalityCode =
request.getParameter("Nationality");
String strNationality;
strNationality with the appropriate text but first we
need to check for null data,
if(strNationalityCode!=null)
{
}
else
{
strNationality = "not specified";
}
if..else if statements that set the users selected
nationality using code similar to
if(strNationalityCode.equals("EU"))
strNationality = "European";
strAgeGroup to the value the user selected. Check the
source HTML to determine what codes were used for the age groups.
String[] strWeekend =
request.getParameterValues("Weekend");
This isn't entirely true. The browser actually sends a series of parameters all with the same name and it is the JSP Container which constructs the array as it analyses the HTTP header received from the client.
astrFlavours
array so that the users choices are displayed in the page. Do the same with the weekend
activities further down.If you couldn't get your solution to work then click the icon to display the source code.
Form processing is a key aspect of JSP work and effective code is essential. There are various tips here and the most important is probably writing code that is aware of null or empty data. The following code is an ideal way of handling these scenarios.
String strParam = request.getParameter("Parameter");
if((strParam!=null)&&(strParam.equals("Value")))
{
...
}
else
{
...
}
A technique for debugging problems with forms is to set the method attribute of the HTML form to "GET". This has the effect of writing the parameter=value pairs as a query string which can then be expected to see if the data you're expecting is actually being sent.
A similar technique to this is to use code to write out all the data sent. The following will do this.
<table>
<%
Enumeration eParams = request.getParameterNames();
while(eParams.hasMoreElements())
{
String strParam = (String)eParams.nextElement();
%>
<tr>
<td><%= strParam %></td>
<td><b><%= request.getParameter(strParam) %></b></td>
</tr>
<%
}
%>
</table>
The next section looks at the response implicit object.