【问题标题】:How to clear browser cache using java [duplicate]如何使用java清除浏览器缓存[重复]
【发布时间】:2012-06-07 06:39:31
【问题描述】:

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

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

谁能告诉我问题出在哪里以及解决这个问题的方法是什么?

【问题讨论】:

    标签: java guice-servlet


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      <script>
          history.forward();
       </script>
      

      将此javascript添加到该注销页面之前的页面并调用。 在标签上添加以下代码

      onLoad="if (location.href.indexOf('reload')==-1) location.replace(location.href+'?reload')"
      

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

      【讨论】:

      • 我已经用过这个了,但是我没用。
      • 需要刷新页面才能生效。请注意,通常会话仅在浏览器窗口关闭时才真正结束。
      • 刷新页面后还是不行
      • 现在尝试使用 session.removeValue() 方法删除一个值。通过传递您需要删除的值的字符串标识符。试试让我知道
      • 你能给我完整的代码吗?
      【解决方案3】:

      编写如下代码,

           //...
           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);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-06
        • 1970-01-01
        • 2015-10-29
        • 2010-11-05
        • 2013-07-17
        • 2011-10-23
        • 2020-11-19
        • 2018-02-20
        相关资源
        最近更新 更多