Redirect to <error-page> on session timeout

[亡魂溺海] 提交于 2021-01-29 11:21:19

问题


I have a standard J2EE web-application running on WebLogic server. I want to handle a session timeout through the meta-data setup in web.xml. I tried to use the following setting

<error-page>
    <error-code>408</error-code>
    <location>/jsp/testErrorPage.jsp</location>
  </error-page>

However it didn't work.


回答1:


Below is the explanation for 408 request timeout,

The Web server (running the Web site) thinks that there has been too long an interval of time between

  1. the establishment of an IP connection (socket) between the client (e.g. your Web browser or our CheckUpDown robot) and the server and
  2. the receipt of any data on that socket, so the server has dropped the connection.

For eg., lets say you are trying to login. But, because of some reasons there is no receipt of any data. Then there is a 408 request timeout.

But in your case you are trying to handle a session timeout. It means, client has not established a connection with the server for a particular period of time (for eg., 30 mins). Means, you are idle without doing any activity for 30 mins. This is the meaning of session-timeout.

For handling session timeout, you must have below code in your web.xml

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

Above code indicates that if you are idle for 30 mins, your session will be destroyed.

Also, you must write a custom listener which implements HttpSessionListener and configure it as a listeners in your web.xml This code will be executed when your session times out. You can try to redirect to your error page in HttpSessionListener#sessionDestroyed method.

If this solution was not what you were looking for, but instead you are looking for actually solving root cause of 408 error, then fixing 408 errors page might be helpful.




回答2:


Session timeout is a server side event and there is no straightforward way to notify browser of server side session time out. Meaning, the browse cannot get a response without any request. That's how HTTP works.

One of ways this typically implemented is as follows:

  1. Have some javascript based function on the browser which periodically checks the server side session via an Ajax Call.
  2. If the session is still valid, do nothing.
  3. If the session timed out, then redirect the user to desired page.


来源:https://stackoverflow.com/questions/18109300/redirect-to-error-page-on-session-timeout

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