2012-06-07 47 views
0

我在会话中遇到问题。当我注销会话是 结束,但当浏览器后退按钮被按下,我得到前一页。我使用的JSP Servlet技术和我的注销代码如下如何使用java清除浏览器缓存

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

给出有谁能够告诉我问题出在哪里,什么将是这个问题的解决方案?

回答

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

将此javascript加入到注销页面和调用之前的页面。 添加下面的代码在标签

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

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

+0

我已经用这个,但我没有工作。 – user1407310

+0

你需要刷新页面才能生效。请注意,通常该会话仅在浏览器窗口关闭时才真正结束。 –

+0

刷新页面后也不起作用 – user1407310

1

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

0

WIRTE你的代码如下,

 //... 
    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); 
    } 
相关问题