如何在JSP中正确发送会话超时消息?

如何在JSP中正确发送会话超时消息?

问题描述:

使用JSP的默认<session-timeout>设置,我想在会话过期后显示您的会话已过期(30分钟)"消息,并将用户带回登录页面.

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.

要使用((HttpServletRequest) request).getSession(false) == null来检测它未正确完成工作,因为在用户首次进入应用程序时会话将为null,从而使消息立即弹出.

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.

如何避免这种情况?有没有一种方法可以自定义JSP的HttpSession,这样我就可以知道真正的事件何时发生?

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

如何设置表示最后一次登录的cookie?如果cookie有效,并且URL不是登录URL,则可以显示会话超时消息.

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.