问题:在 Form Authentication 时如何实现用户 Logout?
解决:
Step 1. 首先设一个 Logout 的 Servlet
@WebServlet("/api/logout")
public class Logout extends HttpServlet(){
protected void doGet(...){
request.logout(); // 这个比 session.invalidate() 好用
response.sendRedirect(request.getContextPath()); // 退出后重定向到首页
}
}
Step 2. 然后,在页面上加链接:
<a href='api/logout'>退出</a>
就可以了。
本文介绍了一种在FormAuthentication中实现用户登出的方法。主要步骤包括创建一个Logout Servlet,该Servlet通过调用request.logout()方法来注销当前会话,并使用response.sendRedirect()将用户重定向至首页。此外,还在页面上添加了一个链接,方便用户点击后执行登出操作。

被折叠的 条评论
为什么被折叠?



