常用的方法是在jsp页面中添加
response.setHeader( "Pragma ", "No-cache ");
response.setHeader( "Cache-Control ", "No-cache ");
response.setDateHeader( "Expires ", 0);
但是我想让每一个web应用中的所有页面都不缓存,除了在每个jsp页面中添加以上代码外还有其他的方法吗?我考虑过用Servlet Filter,但filter中的参数是ServletResponse而不是ServletHttpResponse,无法设置Header。
------解决方案--------------------
filter中的参数是ServletResponse
--------------
你判断一下不就完了嘛,
if(response instanceof ServletHttpResponse) {
ServletHttpResponse res = (ServletHttpResponse)response;
res.setHeader(...)
}
------解决方案--------------------
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
((HttpServletResponse) response).setHeader( "Cache-Control ", "no-cache "); // HTTP
// 1.1
((HttpServletResponse) response).setHeader( "Pragma ", "no-cache "); // HTTP
// 1.0
((HttpServletResponse) response).setDateHeader( "Expires ", 0);
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}