jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question

How can I avoid making multiple HTTP calls for content that hasn't changed? Is it possible to cache it? Is there a way to set this in the header?

Derived from A question posed by Shridhar N
Topics Java:API:Servlets:Performance, Java:API:JSP:Performance
Author Byron Tymvios
Created Aug 31, 2006


Answer

This can be achieved by using a filter. You can setup the filter in your web.xml to intercept the response and edit the HTTP headers for the specified content type. An example filter could look like:

package com.xyz

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class CacheFilter implements javax.servlet.Filter {
  FilterConfig filterConfig = null;

  public void init(FilterConfig filterConfig){
    this.filterConfig = filterConfig;
  }

  public void doFilter(ServletRequest req,
     ServletResponse res,
     FilterChain chain)
     throws IOException, ServletException {
    String sCache = filterConfig.getInitParameter("cache");

    if(sCache != null){       ((HttpServletResponse)res).setHeader("Cache-Control", sCache);

    }

    chain.doFilter(req, res);
  }

  public void destroy(){
    this.filterConfig = null;
  }
}

Now to set up this filter to act on all jpg requests you need to add the following to your web.xml file:

<filter>
  <filter-name>Cache</filter-name>
  <filter-class>com.xyz.CacheFilter</filter-class>
  <init-param>
    <param-name>cache</param-name>
    <param-value>public, max-age=2592000</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>Cache</filter-name>
  <url-pattern>*.jpg</url-pattern>
</filter-mapping>

This filter will now instruct the client to store the specified content (jpg) in it's cache for 2592000 seconds from when this request was processed and no requests for this resource will be necessary till the time has elapsed.



Is this item helpful?  yes  no     Previous votes   Yes: 3  No: 0



Comments and alternative answers

Comment on this FAQ entry

How can I avoid making multiple HTTP calls for content that hasn't changed? Is it possible to cache it? Is there a way to set this in the header?
vijay saradhi, Oct 23, 2006  [replies:2]
Does this filter work for .css files and .js files also?

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re: How can I avoid making multiple HTTP calls for content that hasn't changed? Is it possible to cache it? Is there a way to set this in the header?
Byron Tymvios, Oct 23, 2006  [replies:1]

This filter will work for whatever file types you specify. Simply change the url-pattern attribute or add new ones. ie:

<filter>
  <filter-name>Cache</filter-name>
  <filter-class>com.xyz.CacheFilter</filter-class>
  <init-param>
    <param-name>cache</param-name>
    <param-value>public, max-age=2592000</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>Cache</filter-name>
  <url-pattern>*.css</url-pattern>
</filter-mapping>

OR
<filter>
  <filter-name>Cache</filter-name>
  <filter-class>com.xyz.CacheFilter</filter-class>
  <init-param>
    <param-name>cache</param-name>
    <param-value>public, max-age=2592000</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>Cache</filter-name>
  <url-pattern>*.jpg</url-pattern>
</filter-mapping>
<filter-mapping>
  <filter-name>Cache</filter-name>
  <url-pattern>*.css</url-pattern>
</filter-mapping>



Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Reply to this answer/comment  Help  
Re[2]: Is it possible to cache it? Is there a way to set this in the header?
Vent Visor, Aug 10, 2007
Caching would be useless if it did not significantly improve performance. The goal of caching in HTTP/1.1 is to eliminate the need to send requests in many ladder bar cases, and to eliminate the need to send full responses in many other cases. The former reduces the number of network round-trips required for many operations; we use an "expiration" mechanism for this purpose (see section 13.2). The latter reduces network bandwidth requirements; we use a "validation" mechanism for this purpose (see section 13.3).

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Cache Filter and 404 handling
Sri Ch, Dec 6, 2007  [replies:1]
We are trying to improve the performance of an existing application by adding caching filter. After adding the filter the IE crashes occassionally if the response html has invalid file paths for gif, js, css. How do i check the status before adding the max age parameter to header.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re: Cache Filter and 404 handling
Vent Visor, Feb 12, 2008
public function __call($name, $parameters)
{
self::setHeaderStatus('404 Not Found'); // set HTTP response status
$this->view->request = Zend_Debug::dump($this->getInvokeArg('origRequest'), null, false);
$this->renderToSegment('body', 'error404'); // render view script 'error404' to response segment 'body'
}
Try this code to check the status before adding the max age parameter to header. Good luck...

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
It worked fine.
aromal C, Jun 29, 2009
Kudos to Byron. Thank you..

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
IE settings and Cache filter
Muguntha Kumar, Jul 3, 2009
In IE-6, Internet Options - General - Temporary Internet Files - Settings - Check for newer versions of stored pages is set to option 'Automatically'.

In this case, even if the static contents are cached, the browser checks to see if there are newer versions of stored pages, thereby the 304 responses. If the setting 'Check for newer versions of stored pages' is set to 'Never', the static contents would be fetched from the cache and thereby 304's will not appear anyway. So what difference would this filter make, if it is implemented in web app and the Check for newer versions of stored pages is set to option 'Automatically'.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  

thank you!


Ask A Question



 
Related Links

Servlets FAQ

Servlets Forum

Servlet-related resource list from Purple Technology

jGuru JSP FAQ

Sun Servlet Home Page

JSP FAQ

JSP Forum

Sun's JSP home page

SUN's JSP-Interest mailing archive

IBM's "Introduction to JSP" online course

JSP Insider

java.isavvix.com

Wish List
Features
About jGuru
Contact Us

 


Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers