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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10926833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:06:35  来源:igfitidea点击:

How to clear browser cache using java

javaguice-servlet

提问by user1407310

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

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

                    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?

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

回答by tom

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.

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

回答by Sai Ye Yan Naing Aye

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

回答by Alpesh Prajapati

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

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

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

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

It will solve your back button issue.

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