How to properly message a session timeout in JSP?

坚强是说给别人听的谎言 提交于 2019-12-06 12:49:38

问题


Using the default <session-timeout> setting for JSP, I want to show an "Your session expired (30 min)" message after the session expire and bring the user back to the login page.

To use ((HttpServletRequest) request).getSession(false) == null to detect it isn't doing the job properly, because the session will be null already at the first time the user enters the application, making the message pop right on.

How can I avoid this? Is there a way to customize JSP's HttpSession so I could know when the real event happened?


回答1:


How about setting a cookie indicating last login? If the cookie is valid and url is not login url, then the session timeout message can be shown.




回答2:


How about using request.isRequestedSessionIdValid() method (pseudo code):

if(request.getRequestedSessionId() == null) {
  // no session yet -> need to create new one
}
else if(request.isRequestedSessionIdValid()) {
  // we have valid session
}
else {
  // session invalid due to timeout -> handle timeout
}


来源:https://stackoverflow.com/questions/10051828/how-to-properly-message-a-session-timeout-in-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!