5.8 Redirecting a Response

   

Before JSTL, the only way to redirect an HTTP response in a Java-based Web application was to use the HttpServletResponse.sendRedirect method. JSTL makes redirecting HTTP responses much easier with the <c:redirect> action, as illustrated by the application shown in Figure 5-6.

Figure 5-6. Use <c:redirect> to Log Access to External Resources

graphics/05fig06.jpg

The application shown in Figure 5-6 logs access to external resources, which are JavaWorld articles that discuss Java design patterns. The application consists of two JSP pages. One of those JSP pages, shown in the top picture in Figure 5-6, uses the <c:url> and <c:param> JSTL actions, in conjunction with HTML anchor element, to provide links to five JavaWorld articles. Instead of pointing directly to the articles, those links point to a second JSP page that's passed the article's URL as a request parameter. The second JSP page, which is not shown in Figure 5-6, logs information about the links that were selected in the first JSP page and redirects the HTTP response to the JavaWorld article in question. The bottom pictures in Figure 5-6 show two of those articles.

The second JSP page sends information to the standard servlet log; that information looks like this:

 Remote host 127.0.0.1 accessed Decorator Design Pattern article on Wed Jun 12 13:31:09 graphics/ccc.gif MDT 2002  ... Remote host 127.0.0.1 accessed Composite Design Pattern article on Wed Jun 12 13:31:44 graphics/ccc.gif MDT 2002 

The information stored in the log file provides information about the remote host that accessed the article, the name of the article, and the date and time when the access occurred.

The JSP page shown in the top picture in Figure 5-6 is listed in Listing 5.10.

For readability, the preceding JSP page uses <c:set> actions to create scoped variables that reference the JavaWorld article URLs. Subsequently, the JSP page uses <c:url> with enclosed <c:param> actions to create five URLs that all point to a JSP page named log_access.jsp . Those URLs all contain a request parameter named page whose value represents the JavaWorld article's URL and a request parameter named name that represents the name of the article. Finally, the JSP page creates five HTML anchor elements that reference the URLs that point to log_access.jsp . That JSP page is listed in Listing 5.11.

Listing 5.10 Creating Article URLs
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>    <head>       <title>Using &lt;c:redirect&gt;</title>    </head>    <body>       <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>       <%-- Define a prefix variable for readability --%>       <c:set var='prefix'          value='http://www.javaworld.com/javaworld/'/>       <%-- The base URL for the overview article --%>       <c:set var='overview'        value='${prefix}jw-10-2001/jw-1012-designpatterns_p.html'/>       <%-- The base URL for the strategy article --%>       <c:set var='strategy'        value='${prefix}jw-04-2002/jw-0426-designpatterns_p.html'/>       <%-- The base URL for the decorator article --%>       <c:set var='decorator'        value='${prefix}jw-12-2001/jw-1214-designpatterns_p.html'/>       <%-- The base URL for the proxy article --%>       <c:set var='proxy'        value='${prefix}jw-02-2002/jw-0222-designpatterns_p.html'/>       <%-- The base URL for the composite article --%>       <c:set var='composite'        value='${prefix}jw-12-2001/jw-1228-jsptemplate_p.html'/>       <%-- The encoded URL for the overview article --%>  <c:url var='overviewUrl' value='log_access.jsp'>   <c:param name='page' value='${overview}'/>   <c:param name='name' value='Design Patterns Overview'/>   </c:url>  <%-- The encoded URL for the strategy article --%>  <c:url var='strategyUrl' value='log_access.jsp'>   <c:param name='page' value='${strategy}'/>   <c:param name='name' value='Strategy Design Pattern'/>   </c:url>  <%-- The encoded URL for the decorator article --%>  <c:url var='decoratorUrl' value='log_access.jsp'>   <c:param name='page' value='${decorator}'/>   <c:param name='name' value='Decorator Design Pattern'/>   </c:url>  <%-- The encoded URL for the proxy article --%>  <c:url var='proxyUrl' value='log_access.jsp'>   <c:param name='page' value='${proxy}'/>   <c:param name='name' value='Proxy Design Pattern'/>   </c:url>  <%-- The encoded URL for the composite article --%>  <c:url var='compositeUrl' value='log_access.jsp'>   <c:param name='page' value='${composite}'/>   <c:param name='name' value='Composite Design Pattern'/>   </c:url>  <font size='5'>          Here are some JavaWorld articles on Java Design Patterns:       </font><p>       <%-- Links to articles with URLs created above --%>       <a href='<c:out value="${overviewUrl}"/>'>          Java Design Patterns Overview       </a><p>       <a href='<c:out value="${strategyUrl}"/>'>          The Strategy Design Pattern       </a><p>       <a href='<c:out value="${decoratorUrl}"/>'>          The Decorator Design Pattern       </a><p>       <a href='<c:out value="${proxyUrl}"/>'>          The Proxy Design Pattern       </a><p>       <a href='<c:out value="${compositeUrl}"/>'>          J2EE Composite View Pattern       </a>    </body> </html> 
Listing 5.11 log_access.jsp
 <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %> <%-- Log a message in the log file --%> <% application.log("Remote host " + request.getRemoteHost() +                    " accessed " + request.getParameter("name") +                    " article on " + new java.util.Date()); %> <%-- Redirect the response to the specified page --%>  <c:redirect url='${param.page}'/>  

The preceding JSP page uses a scriptlet to write a message to the application log file and subsequently uses <c:redirect> to redirect the response to the JavaWorld article.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

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