use*_*926 1 java spring jsp servlets mybatis
我已经为酒店管理员的模拟创建了一个登录页面.现在我想为它添加会话时间功能.换句话说,假设用户离开计算机(他仍然登录管理网页)大约10分钟左右.然后,当他回来时,我想结束当前会话,然后重定向到登录页面(这更安全,他的个人信息永远不会丢失!).
我该如何做到这一点?
public class LoginServlet extends SpringInjectedServlet {
@Autowired
private LoginService loginService;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String id = req.getParameter("id");
String password = req.getParameter("password");
String error = null;
//code for checking correct input
}
// mock
private boolean check(String id, String password) {
return loginService.authenticate(id, password);
}
@Override
public void init() throws ServletException {
System.out.println("LoginServlet");
}
}
Run Code Online (Sandbox Code Playgroud)
使用身份验证筛选器在检查会话的每个请求像
HttpSession session = request.getSession();
if (session == null || session.getAttribute("username") == null) {
// Forward the control to login.jsp if authentication fails or session expires
request.getRequestDispatcher("/login.jsp").forward(request,
response);
}
Run Code Online (Sandbox Code Playgroud)
这将检查每个请求的会话登录用户名,如果它的null或会话已过期,它将重定向到登录页面.
在web.xml中添加它
<session-config>
<session-timeout>5</session-timeout>
</session-config>
Run Code Online (Sandbox Code Playgroud)
检查它在这里.