Recipe 6.5 Including Content in a JSP Each Time the JSP Handles a Request


Problem

You want to include content in a JSP each time it receives a request, rather than when the JSP is converted to a servlet.

Solution

Use the jsp:include standard action.

Discussion

The jsp:include action includes a resource in a JSP each time it receives a request, which makes jsp:include more of a dynamic include mechanism than the include directive (see Recipe 6.4). Using jsp:include , the included JSP segments have access to the including page's request , session , and application implicit objects, and to any attributes these objects contain. Use the jsp:include action in each location of the file where you need to import resources such as JSP segments from the same web application.

The import custom action, which is part of the core JSTL, can import resources from other web applications or from other locations on the Internet. See Recipe 6.8.


Example 6-10 shows a JSP page that receives submitted form information from another page in the web application. The receiving page uses jsp:include to include header and footer page segments at the top and bottom of the page.

Just to show that the included segments have access to the same request and session information as their parent page, the header segment displays the person's submitted name , which is stored in fname and lname request parameters, in the form of a greeting- related title HTML tag. The footer page element displays the session ID along with the user's submitted first and last name. First, Figure 6-6 shows an HTML page with a simple submission form for the user 's first name, last name, and email address.

Figure 6-6. An HTML form
figs/jsjc_0606.gif

Assume that this form page includes embedded JavaScript to check the validity of the entered information. When the user clicks the Submit button, the form information is submitted with the following HTML tag to /solutions.jsp :

 <form method=post action="/solutions.jsp"> 

Example 6-11 shows the solutions.jsp page, which includes two JSP segments: header.jspf and footer.jspf . The header.jspf contains the contents of a head HTML tag, and places the user's submitted name in its nested title tag. The footer.jspf page ”for the sake of demonstration ”echoes the user's name and shows his session ID, which it obtains from the implicit session JSP object. The JSP 2.0 specification recommends that you keep these files in WEB-INF/jspf .

Example 6-11. Including two page segments and displaying submitted form values
 <%@page contentType="text/html"%> <html>  <jsp:include page="/WEB-INF/jspf/header.jspf" />  <body bgcolor="white"> <table width="660" border="0" summary="A two-column table in which resides a logo and  navigation bar">       <tr><td valign="top">             Organization image goes here...<p>       <u>Main</u>       </td>       <td align="right" valign="top">       Navbar goes here...       </td></tr><tr><td valign="top" align="center" colspan="2">       <table border="0" summary=         "A nested table for aligning body content">        <tr><td><h2>Thanks for registering at this site</h2></td></tr>         <tr><td>Here is the info you submitted:</td></tr>         <tr><td>Name:           <%= request.getParameter("fname") %>           <%= request.getParameter("lname") %></td></tr>          <tr><td>Email:            <%= request.getParameter("eaddress") %> </td></tr></table>       </td></tr><tr><td></td></tr>       </table> <table width="660" border="0" summary=     "A table containing a footer navigation bar."> <tr><td valign="top" align="center">  <jsp:include page="/WEB-INF/jspf/footer.jsp" />  </td></tr> </table> </body> </html> 

Example 6-12 shows the header.jspf JSP segment.

Example 6-12. A JSP header segment included with jsp:include
 <HEAD>     <META name="author" content=         "Bruce W. Perry, author@jspservletcookbook.com">     <META name="keywords" content=         "Java, JSP, servlets, databases, MySQL, Oracle, web development">     <TITLE>Parker River: Thanks For Visiting  <%= request.getParameter("fname") %>        <%= request.getParameter("lname") %>  </TITLE> </HEAD> 

All this segment does is include the user's name in the title tag. Example 6-13 shows the imported footer.jspf segment. This segment also writes the user's name to the displayed output and adds the session ID, after checking whether the javax.servlet.http.HttpSession object is null , and before it calls the HttpSession.getId( ) method.

Example 6-13. A JSP footer segment included with jsp:include
 Thanks for visiting  <%= request.getParameter("fname") %>      <%= request.getParameter("lname") %><br>  Session ID:  <% if (request.getSession( ) != null)  {%>         <%= request.getSession( ).getId( ) %>     <% } else {%>         Unknown     <% } %> <br><br>  <a href="/index.html">Main</a>  <a href="/service.html">Services</a>     <a href="/sitemap.html">Site Map</a>       <a href="/resources.html">Resources</a>         <a href="/contacts.html">Contact Us</a>          <a href="/prns_privacy.html">Privacy</a> 

Figure 6-7 shows the solutions.jsp page in a web browser.

Figure 6-7. The included header and footer segments displayed in a web browser
figs/jsjc_0607.gif

Using jsp:include , changes to included files are reflected immediately in the including pages. On the other hand, if you make changes to a page that is included using the include directive, those changes are not reflected in the including page until you modify that page and force the JSP container to recompile it.


See Also

Recipe 6.4 on the include directive; Recipe 6.7 on including JSP segments in XML files; Recipe 6.8 on including content from outside of a JSP's context; Chapter JSP.1.10.3 of the JSP 2.0 specification on including files in JSPs; Chapter 23 on the JSTL tags.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net