2

What i want when the session will expired in two minute then i want to transfer user back to the login page so what i need to do?

Currently I have written this line of code to invalidate the session in web.xml.

<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<session-config>
<session-timeout>2</session-timeout>
</session-config> 

</web-app>
  • 1
    possible duplicate: [Tomcat 7 : Redirect URL in case of session timeout](http://stackoverflow.com/questions/22416645/tomcat-7-redirect-url-in-case-of-session-timeout) – PbxMan Nov 21 '14 at 10:49

2 Answers2

1

You can implement sessionListener and in that you do redirect in case of session expire.Refer this link http://www.mkyong.com/servlet/a-simple-httpsessionlistener-example-active-sessions-counter/

Amit Jain
  • 143
  • 8
  • You can't make the redirection with a session Listener when the session was expired. Because the client has namely not sent any HTTP request at that moment which you could then respond with a redirect. source : http://stackoverflow.com/questions/10943016/how-to-redirect-to-the-login-page-when-the-session-expires – bilelovitch Sep 29 '15 at 14:37
0

You can add this at the top on your doPost() or doGet() functions

HttpSession session = request.getSession(false);// don't create new Session
if(session == null || session.isNew()) {
     response.sendRedirect("/yourPage.jsp");
} else {
    //Do whatever you waana do
}

For eg.

doPost(){
    HttpSession session = request.getSession(false);// don't create new Session
    if(session == null || session.isNew()) {
      response.sendRedirect("/yourPage.jsp");
    } else {
       out.println("I found old session");
    }
}
Anurag Anand
  • 500
  • 1
  • 7
  • 13
  • @Error_Exception You cannot do directly in web.xml. You will have to specify a `Filter` which will check for the state of session. And map that filter in `web.xml`. In this way you will have write code in only one file. But you cannot do it using `web.xml` only. At least you will have to write a filter. – Anurag Anand Nov 21 '14 at 12:25