I want the .png files on my webpage to be cached. I added the following entry in web.xml
<filter> <filter-name>ContentFilter</filter-name> <filter-class>filters.ContentFilter</filter-class> <init-param> <description>Add an Expires Header</description> <param-name>expiryDate</param-name> <param-value>Fri, 30 Apr 2021 20:00:00 GMT</param-value> </init-param> </filter> <filter-mapping> <filter-name>ContentFilter</filter-name> <url-pattern>*.png</url-pattern> </filter-mapping>
Set the value of expiryDate in the following order to init ()
String expiryDateStr = filterConfig.getInitParameter("expiryDate"); SimpleDateFormat format = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z"); try { Date d = format.parse(expiryDateStr); expiryDate = d.getTime(); } catch (ParseException e) { logger.error(e.getMessage(), e); }
doFilter ():
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filChain) throws IOException, ServletException { logger.debug("doFilter()"); logger.info(((HttpServletRequest)req).getRequestURL().toString()); filChain.doFilter(req, res); if (res instanceof HttpServletResponse) { HttpServletResponse response = (HttpServletResponse) res; logger.info(((HttpServletRequest)req).getRequestURL().toString()); response.setDateHeader("Expires", expiryDate); } }
My problem is that whenever I refresh a webpage in a browser, the client keeps requesting .png files. Guess my filter is not working. Is this configuration correct?
source share