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
 |
 |
 |
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
|
|

|
 |
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
|
|

|
 |
 |
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
|
|

|
|
|
 |
|