Fra*_*ank 9 javascript java servlets
我正在使用servlet开发一个java web应用程序,为了防止用户点击后退按钮查看以前用户的信息,我有以下代码:
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
HttpSession session=request.getSession(true);
response.setContentType("text/html");
response.setHeader("Cache-Control","no-cache,no-store");
response.setDateHeader("Expires",0);
response.setHeader("Pragma","no-cache");
......
// if (!User_Logged_In)
session.invalidate();
}
Run Code Online (Sandbox Code Playgroud)
此外,我在文件中还有以下代码:web/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
......
<filter>
<filter-name>ResponseHeaderFilter</filter-name>
<filter-class>ResponseHeaderFilter</filter-class>
<init-param>
<param-name>Cache-Control</param-name>
<param-value>private,no-cache,no-store</param-value>
</init-param>
<init-param>
<param-name>Pragma</param-name>
<param-value>no-cache</param-value>
</init-param>
<init-param>
<param-name>Expires</param-name>
<param-value>0</param-value>
</init-param>
</filter>
</web-app>
Run Code Online (Sandbox Code Playgroud)
ResponseHeaderFilter.java看起来像这样:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ResponseHeaderFilter implements Filter
{
FilterConfig fc;
public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain) throws IOException,ServletException
{
HttpServletResponse response=(HttpServletResponse)res;
for (Enumeration e=fc.getInitParameterNames();e.hasMoreElements();) // Set the provided HTTP response parameters
{
String headerName=(String)e.nextElement();
response.addHeader(headerName,fc.getInitParameter(headerName));
}
chain.doFilter(req,response); // Pass the request/response on
}
public void init(FilterConfig filterConfig)
{
this.fc=filterConfig;
}
public void destroy()
{
this.fc=null;
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,它仍然无法正常工作.后退按钮将显示一个警告窗口,说明数据已过期,它会询问用户是否要重新发布.如果您选择是,它仍将显示以前的页面信息.我究竟做错了什么?有什么问题?
坦率
是的,我正在公共场所为PC开发一个网络应用程序,如果用户B点击后退按钮,他可能会看到用户A的私人信息.
我试图使用servlet的session id,但是怎么做,任何示例代码?
我也尝试过以下方法:
<Html>
<Head>...</Head>
<Body onLoad=document.execCommand("ClearAuthenticationCache","false")>
......
<script type="text/javascript">
// Clear current credentials : Requires IE6 SP1 or later
// document.execCommand("ClearAuthenticationCache");
document.execCommand("ClearAuthenticationCache","false");
</script>
......
</Html>
Run Code Online (Sandbox Code Playgroud)
它适用于IE,但Firefox.
Cla*_*diu 12
如何点击后退按钮会导致用户看到其他用户的数据?你的用例是什么?它是为公共终端设计的,每个用户提交数据然后离开?在这种情况下,将每个输入与唯一的会话ID相关联.跟踪服务器中的有效会话ID.提交输入后,从有效ID中删除该会话ID.如果再次出现,则不显示信息.
cat*_*ood 10
你的问题是你试图让客户看不到他或她自己的电脑上的内容.你不能阻止他们查看他们的浏览器缓存.您不能阻止它们禁用JavaScript(以及您的脚本代码).您不能阻止他们使用不遵守您提及的"重新发布"惯例的浏览器.
这不是可以通过JavaScript或服务器端解决方案解决的问题.为什么"打破后退按钮"的部分原因令人不悦:它实际上并没有解决任何问题.
| 归档时间: |
|
| 查看次数: |
3371 次 |
| 最近记录: |