How to clear browser cache using java [duplicate]

China☆狼群 提交于 2019-12-02 20:49:10

问题


I have a problem in sessions. When ever I logout the session is ended but then when the browsers back button is pressed I am getting the previous page. I am using jsp servlet technology and my code for logout is given below

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

can anybody tell me where is the problem and what will be the solution for this problem?


回答1:


Do you set the cache headers only on the logout page? If so, you need to put those on each page since the page you were coming from does not have them and will be cached.




回答2:


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

Add this javascript to the page before that logout Page and call. Add below code on tag

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

It will solve your back button issue.




回答3:


Wirte your code as below,

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


来源:https://stackoverflow.com/questions/10926833/how-to-clear-browser-cache-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!