Java – Redirect to on session timeout

javaservletssession-timeout

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.

Best Answer

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.