我正在使用servlet建立我的网站并在发布会话时遇到问题,请检查并找出错误:
login.java :
ResultSet res=smt.executeQuery("Select * from admin where name='"+user+"' and password='"+password+"'");
while(res.next())
{
String name=res.getString("name");
String pwd=res.getString("password");
if(category.equals("Admin"))
{
if(name.equals(user) && pwd.equals(password))
{
ServletContext context=request.getServletContext();
context.setAttribute("name", name);
HttpSession session=request.getSession();
session.setAttribute("name", name);
RequestDispatcher view=request.getRequestDispatcher("welcome_admin.jsp");
view.forward(request, response);
}
else
{
response.sendRedirect("index.jsp");
}
}
并且logout.jsp是:
<body>
<%
session.removeAttribute("name");
session.invalidate();
%>
<b>
<%
RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.include(request, response);
%>
</b >
</body>
但会话未发布,如果我点击后退按钮,它会再次将我重定向到页面!有人帮帮我!!
答案 0 :(得分:3)
这是因为您的浏览器缓存了该页面,它不再来自服务器,您需要instruct your browser not to cache pages by adding a Filter
答案 1 :(得分:1)
问题在于缓存。
解决方法是将其添加到您的servlet:
Java servlet how to disable caching of page
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");