I am using filter that check if user is log in then don’t cache the previous page. The code for this is like,
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
....
if (isRegisteredUser.equalsIgnoreCase(("1"))) {
sessionID = UUID.randomUUID().toString();
session.setMaxInactiveInterval(1800);
Cookie userCookie = new Cookie("userCookie", "loginUser");
userCookie.setPath("/");
httpServletResponse.addCookie(userCookie);
Cookie sessionCookie = new Cookie("WITSessionCookie", sessionID);
sessionCookie.setMaxAge(60*30);
sessionCookie.setPath("/");
httpServletResponse.addCookie(sessionCookie);
if (!httpServletRequest.getRequestURI().startsWith(httpServletRequest.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
httpServletResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
httpServletResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
httpServletResponse.setDateHeader("Expires", 0); // Proxies.
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
...
} //end of doFilter()
But the problem is, if on any page user click on any button or link, and I return null for that action, and then I click on browser back button, then it says page expires. How can I prevent this? It is fine, that the page is not caching, but why it expires my page when I click on any button or any link and return null for that action?
Thanks
That’s normal and specified behaviour when the enduser attempts to re-send a POST request which is not cached in the browser. You need to implement the post-redirect-get pattern. Send a redirect to the destination page in the action method. Or, better, make it an ajax submit instead. This is by the way regardless of whether you return to the same view by returning
nullor not.