Java 如何从servlet清除浏览器缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19923045/
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-08-12 21:33:49  来源:igfitidea点击:

How to clear browser cache from servlet?

javajspservletscache-control

提问by Hareesh

When I'm clicking logout button I want the browser cache to be cleared.

当我单击注销按钮时,我希望清除浏览器缓存。

I'm trying to clear the browser cache, using the following code:

我正在尝试使用以下代码清除浏览器缓存:

if(uri.equals("/SciArchive/logout.sci"))
    {
        HttpSession session=req.getSession(false);
         res.setHeader("Cache-Control","no-cache"); 
             res.setHeader("Cache-Control","no-store"); 
             res.setDateHeader("Expires", 0); 
             res.setHeader("Pragma","no-cache"); 
        if(session!=null)
        {
            session.invalidate();
            rd=req.getRequestDispatcher("/home.jsp");               
        }

        rd.forward(req,res);
        return;
    }

When back button is pressed in browser window after logout, I need the browser not showing the previous page.

注销后在浏览器窗口中按下后退按钮时,我需要浏览器不显示上一页。

When I'm clicking back button it shows the previous page, after clicking forward button in browser window it shows document expired.

当我单击后退按钮时,它显示上一页,单击浏览器窗口中的前进按钮后,它显示document expired.

Please give me suggestions to clear this error!

请给我建议以清除此错误!

采纳答案by ljgw

You cannot clear the cache, but you can ask that your pages will not be cached. This means that you must do so for every page in your session (else the back-button will keep working, and your logout page is the only page not cached).

您无法清除缓存,但您可以要求不缓存您的页面。这意味着您必须对会话中的每个页面都这样做(否则后退按钮将继续工作,并且您的注销页面是唯一未缓存的页面)。

See also: How to control cache in JSP page?:

另请参阅: 如何控制 JSP 页面中的缓存?

httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
httpResponse.setDateHeader("Expires", 0); // Proxies.
httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
httpResponse.setDateHeader("Expires", 0); // Proxies.

edit: Apparently it is possible to clear the cacheprogrammatically using javascript and a refresh, but I doubt that this is what you are looking for: How to programmatically empty browser cache?

编辑:显然可以使用 javascript 和刷新以编程方式清除缓存,但我怀疑这是否是您要查找的内容: 如何以编程方式清空浏览器缓存?

回答by NAV PALLAV

<%
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 enter code here

String userName = (String) session.getAttribute("plno");
if (null == userName) {
  //request.setAttribute("Error", "Session has ended.  Please logenter code herein.");
  RequestDispatcher rd = request.getRequestDispatcher("Login.jsp");
  rd.include(request, response);
  out.println("Session has ended.  Please login.");
}
%>

This is how to clear the cache in jsp page. It can also set the session's inactive timeout

这就是清除jsp页面缓存的方法。它还可以设置会话的非活动超时