Using HTTP Request Parameters

The next requirement for many JSP pages is to be able to use request parameters to configure the behavior of the page. Using the Agency case study as an example, you will develop a simple JSP to display the contents of a named database table.

The first step is to define a simple form to allow the user to select the table to display. Listing 13.7 shows a simple form encoded as a JSP.

Listing 13.7. Full Text of tableForm.jsp
 1: <HTML>
 2: <TITLE>Agency Tables</TITLE>
 3: <BODY>
 4:   <FORM action=table>
 5:     Select a table to display:
 6:     <SELECT name=table>
 7:       <OPTION>Applicant
 8:       <OPTION>ApplicantSkill
 9:       <OPTION>Customer
10:       <OPTION>Job
11:       <OPTION>JobSKill
12:       <OPTION>Location
13:       <OPTION>Matched
14:       <OPTION>Skill
15:     </SELECT><P>
16:     <INPUT type=submit>
17:   </FORM>
18: </BODY>
19: </HTML>
					

Although the form in Listing 13.7 contains only HTML, it is convenient to treat it as a JSP. You can define an alias for this page and deploy it to the same Web resource location as the JSP that will display the table. This will simplify the application deployment and Web site management administration. The form component of the page is set to invoke the JSP called table in the current Web application (see line 4).

You will need to add this JSP to the simple Web application and define an alias of /tableForm to use it.

The actual JSP to display the table is shown in Listing 13.8.

Listing 13.8. Full Text of table.jsp
 1: <%@page import="java.util.*, javax.naming.*, agency.*" %>
 2: <%@page errorPage="errorPage.jsp" %>
 3: <% String table=request.getParameter("table"); %>
 4: <HTML>
 5: <TITLE>Agency Table: <%= table %></TITLE>
 6: <BODY>
 7: <H1>Data for table <%= table %> </H1>
 8: <TABLE border=1>
 9: <%
10:         InitialContext ic = null;
11:         ic = new InitialContext();
12:         AgencyHome agencyHome = (AgencyHome)ic.lookup("java:comp/env/ejb/Agency");
13:         Agency agency = agencyHome.create();
14:         Collection rows = agency.select(table);
15:         Iterator it = rows.iterator();
16:         while (it.hasNext()) {
17:               out.print("<TR>");
18:               String[] row = (String[])it.next();
19:               for (int i=0; i<row.length; i++) {
20:                   out.print("<TD>"+row[i]+"</TD>");
21:               }
22:               out.print("</TR>");
23:         }
24: %>
25: </TABLE>
26: </BODY>
27: </HTML>
					

The JSP in Listing 13.8 uses the implicit JSP variable called request to access the ServletRequest object (see line 3). The request parameters are retrieved by using the getParameter() method on the request object. The rest of this JSP simply creates an instance of the agency Session bean and uses the select() method to retrieve the table data. The data is formatted as an HTML table and output using the implicit JspWriter variable called out.

You will need to add this JSP to the simple web application and define an alias of /table to use it. Because the Web application file already contains an EJB Reference to the agency Session bean (from the name.jsp example in Listing 13.6), you do not need to define this EJB Reference again. If you deploy this Web component in a different Web application, you will need to add the EJB reference shown previously in Figure 13.9.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.32.1