user1281029
user1281029

Reputation: 1533

BrowserCache Removal Not working

/**
 * 
 */


public class NoCacheFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) request;
        HttpServletResponse httpRes = (HttpServletResponse) response;

        if (!httpReq.getRequestURI().startsWith(httpReq.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
            httpRes.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            httpRes.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            httpRes.setDateHeader("Expires", 0); // Proxies.
        }

        chain.doFilter(request, response);
    }

    // ...

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

But it is not working, I still can get pages while pressing the forward and back buttons. Please help.I have already cleared all the exsisting cache.

Upvotes: 0

Views: 434

Answers (1)

BalusC
BalusC

Reputation: 1108722

You should not purposefully throw exceptions during filter's init() and destroy(). If the init() throws an exception, then the filter is not placed in service and some servers will even completely abort the deploy of the webapp.

They should do nothing if you have nothing to implement there.

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    // NOOP.
}

@Override
public void destroy() {
    // NOOP.
}

You also need to make sure that the filter is mapped on the proper URL pattern or servlet name. Easiest is to put a @WebFilter("Faces Servlet") on the class wherein Faces Servlet is the exact servlet name of the FacesServlet entry as you have in web.xml.

Upvotes: 1

Related Questions