2014-05-12 30 views
1

我写了这个代码注销的servlet,但是当我点击后退按钮一切工作fine.Kindly建议注销Servlet的不正常

public class LogOut extends HttpServlet { 


    private static final long serialVersionUID = 1L; 

public void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
PrintWriter out = response.getWriter(); 
HttpSession session = request.getSession(true); 
System.out.println("logging out"); 

HttpSession session1=request.getSession(); 

session1.invalidate(); 

out.print("You are successfully logged out!"); 
RequestDispatcher rd= request.getRequestDispatcher("/Logout.jsp"); 
rd.forward(request, response); 

out.close(); 


} 
} 
+1

该会话与浏览器缓存无关。您需要禁用缓存才能使按钮行为发生更改。 –

回答

0
Session invalidation is enough for a perfect logout servlet.In order to made logout working properly we want to add a concept of validating authentication. 

HttpSession的会议= request.getSession(真);

session.invalidate();

创建login.that包括ID,username..etc.Then检查的验证对象每次用户是 authObj ATH内servlet.After每个操作的开始会话失效 如果你尝试做任何操作,你不会因为我们没有用户名 (作为一个例如:),我们已经保存在会话中。用户名不可用的原因是我们已经完成了会话失效。所以我们不能进行任何操作我们需要登录。然后只需添加如下消息:

PrintWriter out = response.getWriter();

out.println(“SESSION FAILED!”);

0

虽然这样做(用户可以禁用不完美的方式javascript),我们可以使用history.forward()方法强制用户跳回到下一页。但是,由于浏览器不调用在onload方法每次还有其他问题,也我们要修改我们的代码

<html> 
    <head> 
    <title>Page One</title> 
    <script> 
    function backButtonOverride() 
    { 
     setTimeout("backButtonOverrideBody()", 1); 
    } 

    function backButtonOverrideBody() 
    { 

     try { 
     history.forward(); 
     } catch (e) { 

     } 

     setTimeout("backButtonOverrideBody()", 500); 
    } 
    </script> 
    </head> 
    <body onLoad="backButtonOverride()"> 
    <h1>Page One</h1> 
    <a href="page2.html">Move to Page Two</a> 
    </body> 
    </html> 

我会建议,而不是在页面之前的执行这段代码注销页面实现这个代码注销页面本身,并添加JavaScript代码从登出页面用户重定向到“登录页”只是使用增加安全性: -

window.location = url; 
0

在logut的servelt

你需要做适当的这样的

response.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0"); 
response.addHeader("Pragma", "no-cache"); 
response.addDateHeader ("Expires", 0); 

如果您正在使用Cookie,然后你需要清除它们太

/** 
     * Invalidate all cookies by, for each cookie received, 
     * overwriting value and instructing browser to deletes it 
     */ 
     Cookie[] cookies = request.getCookies(); 
     if (cookies != null && cookies.length > 0) { 
      for (Cookie cookie : cookies) { 
       cookie.setValue("-"); 
       cookie.setMaxAge(0); 
       response.addCookie(cookie); 
      } 
     } 

在你Logout.jsp您在头标记加入这些

<head> 
<meta http-equiv="Cache-Control" content="no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0" /> 
<meta http-equiv="Pragma" content="no-cache" /> 
<meta http-equiv="Expires" content="0" /> 
</head>