What s Next


Resin and Caching

The Resin server provides a built-in advanced proxy cache. Per our previous discussion, the Resin proxy cache works with the HTTP headers to determine which pages should be cached. The default setup for the cache is to not cache any pages unless specific HTTP header information is present. The Resin server will not cache pages that use sessions and have cookies associated with them. If your site uses cookies for anonymous or users who don't log into a site, the pages will not be cached. This is an important consideration when you're designing site pages.

One of the Resin cache's powerful features (which most proxy caches don't allow) is the partial caching of JSP pages that contain includes. The includes allow you to write a page like that shown in Figure 14.1 as follows:

 <%@ page session=true %> <% if (! session.isNew()) { %>   <h1>Here's Your Personalized New Page!</h1> <% } %> <jsp:include page="menu.jsp"/> <jsp:include page="body.jsp"/> <jsp:include page="footer.jsp"/> 


Figure 14.1: Included page caching.

This code snippet represents the personalized page called news.jsp. In Figure 14.1, the gray boxes represent pages that can be cached because they aren't personalized. A normal proxy cache can't break apart the pages, but the Resin cache can. The menu.jsp and footer.jsp pages set session=false, so they can be cached by the Resin server. In the remainder of the chapter, we will show you how to set various HTTP header values to facilitate caching in both JSP and servlet code.

Using Expires

If you have pages that don't change often, it is a good idea to let Resin cache the pages in order to boost performance. If changes to the pages are produced on a regular basis, they can still be cached for a period of time—even if the time is short. To achieve page caching for a time period, use the Expires HTTP header. The following listing shows how to set the Expires HTTP header in JSP:

 <%@ page session=false %> <%     long currentTime = System.currentTimeMillis();     response.setDateHeader("Expires", currentTime + 30000); %> <H1>Headline 1</H1> More Text to output 

This code snippet begins by making sure the page session variable is set to false; otherwise the rest of your efforts are useless. Next, you obtain the current time in milliseconds and store the value in the currentTime variable. Now the response object's setDateHeader() method is called, to add the Expires header. The first parameter indicates your desire to use the Expires HTTP header, and the second parameter is the value to be assigned to the Expires. The value is the current time plus 30 seconds, represented in total as milliseconds. The method handles converting the milliseconds into a time string outlined by the HTTP protocol. The remainder of the snippet is HTML and text to use with the page. When this page is served to a client's browser, Resin caches the page for a total of 30 seconds. At the next page request, the server checks the Expires time against the current time and obtains a new copy of the page if the current time is greater.

You can use the same type of code in a servlet. The following snippet shows how:

 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class CacheServlet extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)     throws IOException, ServletException {     long currentTime = System.currentTimeMillis(); response     setDateHeader("Expires", currentTime + 30000);     PrintWriter out = res.getWriter();     out.println("<H1>Your News</H1>");   } } 

Using If-Modified

Although its useful to set a specific expiration date on a file, another useful technique involves caching a page until it is modified on the server. You can use the If-Modified HTTP header to do this.

To use this technique, you override the getLastModified() method of a servlet or a JSP page. Here's what the code might look like for a servlet:

 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class CacheServlet extends HttpServlet {   public long getLastModified(HttpServletRequest request) {     return new       File(request.getRealPath("news.jsp")).lastModified();   }   public void doGet(HttpServletRequest request,                     HttpServletResponse response)     throws IOException, ServletException {     PrintWriter out = res.getWriter();     out.println("<H1>Your News</H1>");   } } 

The getLastModified() method is overridden. Within the method, the path to the server file news.jsp is obtained, a File object is created, and the lastModified() method is called. This method returns the last modified date, which is subsequently returned to the caller. Resin uses the method to determine whether the page on the server has been changed because the page in the cache was cached. A JSP page looks similar, as shown here:

 <%@ page session=false %> <%!   public long getLastModified(HttpServletRequest request)  {     return new File(request.getRealPath("news.jsp")).lastModified();   } %> <H1>Headline 1</H1> More Text to output 

Using Cache-Mapping

If you want to store a page on the browser, you can include an Expires metatag using a server configuration option called cache-mapping. The Expires metatag is applied to pages sent to the browser, but only if some rules are followed:

  • The page must be cacheable.

  • The If-Modified code must be applied as shown in the previous section.

  • The browser Expires will have no effect on pages that automatically set the Expires HTTP header.

The configuration option looks like the following:

 <web-app id='/'>   <cache-mapping url-pattern='*.jpg'                   expires='12h'/> </web-app> 

Here the configuration option is applied to all pages as noted by the id='/' attribute of the <web-app> element. The <cache-mapping> element says to apply the Expires metatag to all files with the extension JPG. The total cache time is 12 hours.

Setting Cache-Control

If you have a page that doesn't use sessions but you absolutely don't want it to be cached, you should use the Cache-Control HTTP header. Set the no-cache attribute with this code:

 response.setHeader("Cache-Control","no-cache"); 

As of this writing, Resin only recognizes the no-cache attribute for the Cache-Control HTTP header. Resin ignores any other header combination for Cache-Control, and the internal cache is disabled. Recognizing and using the other attributes of the Cache-Control HTTP header have been submitted to Caucho as an enhancement request.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

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