如何使用java清除浏览器缓存

时间:2012-06-07 06:39:32

标签: java guice-servlet

我在会话中遇到问题。当我注销会话时 结束但是当按下浏览器后退按钮时,我得到了上一页。我正在使用jsp servlet技术,我的注销代码如下所示

                    request.getSession().invalidate();
        response.setHeader("Cache-Control","no-cache"); 
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma","no-cache"); 
        response.sendRedirect("home.jsp");

任何人都可以告诉我问题在哪里以及这个问题的解决方案是什么?

3 个答案:

答案 0 :(得分:1)

您是否仅在注销页面上设置缓存标头?如果是这样,您需要将这些内容放在每个页面上,因为您来自的页面没有它们并且将被缓存。

答案 1 :(得分:0)

<script>
    history.forward();
 </script>

将此javascript添加到该注销页面之前的页面并调用。 在标记

上添加以下代码
onLoad="if (location.href.indexOf('reload')==-1) location.replace(location.href+'?reload')"

它将解决您的后退按钮问题。

答案 2 :(得分:0)

编写如下代码,

     //...
     response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
     response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
     response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
     response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility

     //can check userId or something likes this.In this sample, i checked with userName.
     String userName = (String) session.getAttribute("User");
     if (null == userName) {
         request.setAttribute("Error", "Session has ended.  Please login.");
         RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
         rd.forward(request, response);
    }