Workshop 5. The Request Object

Objective

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.

Time Available

This workshop should take no longer than 60 minutes.

Request Object Workshop

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.

Demo

  1. From the following link save the target file to your JSP working folder. This is the HTML file that contains the form.

    Download file icon

  2. Create a new JSP called 'request.jsp' and add the following HTML.

    <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.

  3. At the start of your JSP open a new script block.

  4. First we get the first name and last name that was entered in the form,

    String strFirstName =
                    request.getParameter("FirstName");
    String strLastName =
                    request.getParameter("LastName");
    
  5. Next we need to ensure that we have valid data. In this scenario if the user didn't provide a name we want to say so in the form. Add the following code next,

    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.

  6. Next we want to determine the Nationality the user selected. The form passes the Nationality code rather than the actual text. This is determined by the way this particular element was constructed in the form and you may want to check the source HTML for this. Add the following to pick up the the Nationality code,

    String strNationalityCode =
               request.getParameter("Nationality");
    String strNationality;
    
  7. We want to populate strNationality with the appropriate text but first we need to check for null data,

    if(strNationalityCode!=null)
    {
    
    }
    else
    {
      strNationality = "not specified";
    }
    
  8. In the true clause add a series of if..else if statements that set the users selected nationality using code similar to
    if(strNationalityCode.equals("EU"))
                       strNationality = "European";
    
  9. The age group form element can be treated in exactly the same way as nationality above. Add the appropriate code that will set strAgeGroup to the value the user selected. Check the source HTML to determine what codes were used for the age groups.

  10. The last two form elements, the check boxes and the multi-select list box can be treated the same. These form elements send their data as a string array which can be picked up with,
    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.

  11. Add code to your solution to pick up the values from the check boxes and multi-select list box.

  12. Close the script block and move down the JSP to the HTML that states "Your favourite ice cream flavours are." After this line write out the contents of the astrFlavours array so that the users choices are displayed in the page. Do the same with the weekend activities further down.

  13. Save and test your work.

If you couldn't get your solution to work then click the icon to display the source code. Source code icon

Tech Tips

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>

More Implicit Objects

The next section looks at the response implicit object.

Back Next