我想在用户注销后再次登录时在用户的个人资料页面上显示上次访问的时间。我正在使用HttpSession。我有三个仆人
- 登录
- 注销
- 轮廓
我应该在哪里使用long lastTime = session.getLastAccessedTime();
这是代码:
登录
String name = request.getParameter("name");
String password = request.getParameter("password");
if (password.equals("admin123")) {
out.print("Welcome, " + name);
HttpSession session = request.getSession();
session.setAttribute("name", name);
} else {
out.print("Sorry, username or password error!");
request.getRequestDispatcher("login.html").include(request, response);
}
注销
HttpSession session = request.getSession();
session.invalidate();
out.print("You are successfully logged out!");
轮廓
HttpSession session = request.getSession(false);
if (session != null) {
String name = (String) session.getAttribute("name");
long lastTime = session.getLastAccessedTime();
out.print(lastTime + "last login time");
out.print("Hello, " + name + " Welcome to Profile");
注销时在数据库中执行一个存储存储,下次使用它。这是个好方法。我认为如果服务器重新启动,其他方法将不起作用。在用户会话结束时或根据您的要求存储在数据库中。如果你想用户HttpSession侦听器看看 http://www.mkyong.com/servlet/a-simple-httpsessionlistener-example-active-sessions-counter/