gpt4 book ai didi

java - servlet session ,注销后,按下浏览器的后退按钮时,再次显示安全页面

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:56:04 25 4
gpt4 key购买 nike

<分区>

我有一个 servlet 和一个 HTML 页面。如何防止用户在注销后点击浏览器的后退按钮?我在 stackoverflow 中读过同样的问题,但答案是使用浏览器历史记录禁用 java 脚本或使用页面——在 http header 中没有缓存。我们如何使用阻止返回操作的 servlet 来实现它,没有缓存的 http-header 是无用的,因为 Firefox 表示页面在再次刷新两次时已过期显示安全页面。

我已经在某种程度上做了,示例方法只是为了尝试(不是真的)我的用户名和密码从 HTML 页面发布到 servlet,如果密码和用户名正确,servlet 将其存储在 session 中。当再次请求安全页面时,如果 session 存在,则显示安全页面,并且用户从 session 中注销,显示登录页面,所有工作正常,但如果用户点击浏览器的后退按钮,则注销失败。

我们如何防止安全 servlet 在注销后显示内容,然后在浏览器中按下后退按钮?

welcome.html 的源代码

<html>
<body>

<form method="POST" action="Sessionexample">
<div align="center">
<table border="1" style="border-collapse: collapse">
<tr>
<td>Username</td>
<td><input type="text" name="username" size="20"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" size="20"></td>
</tr>
<tr>
<td height="24">&nbsp;</td>
<td height="24">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Submit" name="B1"></td>
</tr>
</table>
</div>
</form>
</body>
</html>

servlet 的源代码

public class Sessionexample extends HttpServlet implements Servlet , Filter {
private static final long serialVersionUID = 1L;
public String username =null, password=null;
public HttpSession session ;
public PrintWriter pw;
int do_get =0 ;
/**
* Default constructor.
*/


public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
response.sendRedirect("welcome.html"); // No logged-in user found, so redirect to login page.
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);
} else {
chain.doFilter(req, res);
}
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

do_get=1;
pw = response.getWriter();
session=request.getSession(false);
try
{
if(request.getParameter("action")!=null)
{
if(request.getParameter("action").equals("logout"))
{

session = request.getSession(true);
session.setAttribute("username", "");
session.setAttribute("password", "");
session.invalidate();
response.sendRedirect("welcome.html");
return;
}
}
else
if(session !=null)
{
if( (String)session.getAttribute(username)!=null)
username = (String)session.getAttribute("username").toString();
if( (String)session.getAttribute("password") !=null)
password =session.getAttribute("password").toString();
pw.write("not new-");
serviced(request,response);
}

}
catch(Exception ex)
{
pw.write("Error-"+ex.getMessage());
}

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

if(request.getParameter("username")!=null && request.getParameter("password")!=null )
{
username = request.getParameter("username").toString();
password = request.getParameter("password").toString();
}

serviced(request,response);

}


protected void serviced(HttpServletRequest request, HttpServletResponse response) throws IOException
{

response.setContentType("text/html");

pw = response.getWriter();
if( username !=null && password !=null)
if( username.equals("admin") && password.equals("a"))
{

try
{

if(do_get==0)
{
session = request.getSession(true);
session.setAttribute("username", "admin");
session.setAttribute("password", "a");
}
pw.write("You are logged in : "+username+" <br/> "+"<a href='?action=logout'><h1> Logout </h1> </a>");

}
catch(Exception ex)
{
response.sendRedirect("welcome.html");

}

}
else
{
response.sendRedirect("welcome.html");
}
else
response.sendRedirect("welcome.html");
}

@Override
public boolean accept(Object arg0) throws IOException {
// TODO Auto-generated method stub
return false;
}

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com