Jsp page getting called from cache rather than getting loaded from server
I am calling a jsp based on 2 parameters which is passed from jsp 1 in this way.Below i pass 2 parameters into 2.jsp and based on these 2 parameters data is displayed in 2.jsp.I have a loop in which i have a number of hrefs like the one i have described below.Each of these href passes a different set of value to 2.jsp.
out.println("<a href=\"2.jsp?prId=" + prog.getId() + count + "\">" + prog.getName() + "</a>");
I retrieve these 2 parameters in 2.jsp using the following lines
count_id = request.getParameter( "country_id" );
prog_id = Integer.parseInt(request.getParameter( "program_id" ));
Based on these 2 parameters i show the corresponding data in 2.jsp
Now i have a back button in 2.jsp and i call 1.jsp in 2.jsp using the following code
<a href="1.jsp"><img src="/image/back.gif" border="0"></a>
The problem is when i use the back button and go back to 1.jsp and select another href like the one i have described above i get the data related to the previous href selected.
I guess the problem is when i request the page is loaded from cache rather than 开发者_如何学编程from the server. Please advice
You just need to instruct the browser to NOT cache the page. You can do this with help of a servlet filter which sets the following response headers:
@WebFilter("*.jsp")
public class NoCacheFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(req, res);
}
// ...
}
Now just map this filter on an URL pattern covering the JSP pages of interest. E.g. *.jsp
or /somefolder/*
(the @WebFilter
in the example does that for all JSPs).
Make sure that you clear the browser cache before testing.
See also:
- How to control web page caching, across all browsers?
精彩评论