简体   繁体   English

servlet清除本地存储

[英]servlet clear local storage

How the servlet can clear localStorage? servlet如何清除localStorage? I try the following code, but it does not clear the localStorage data. 我尝试以下代码,但不会清除localStorage数据。 When user logout account, it will direct to logout.java servlet, destroy session object and clear localStorage: 当用户注销帐户时,它将定向到logout.java servlet,销毁会话对象并清除localStorage:

    / ... /
    request.logout();
    request.getSession().invalidate();
    request.getSession().removeAttribute("student");
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    response.setDateHeader("Expires", 0); // Proxies.
    out.println("<script type=\"text/javascript\">");
    out.println("localStorage.clear();");
    out.println("</script>");
    response.sendRedirect("index.html");
    /..../

Is there any error? 有什么错误吗?

you are calling response.sendRedirect() which sends to the browser an HTTP 302 response code and a Location HTTP header. 您正在调用response.sendRedirect() ,它将向浏览器发送HTTP 302响应代码和Location HTTP标头。 Your browser is almost certainly ignoring any message body written to the response, so your script will not be executed. 您的浏览器几乎可以肯定会忽略任何写入响应的消息正文,因此不会执行您的脚本。

The part of your code that you are closing tag 您要关闭的代码部分

out.println("</javascript>");

Is the issue. 是问题。 You have opened <script> so that close it </script> . 您已经打开<script>以便将其关闭</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM