Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to Login page when Session is expired in Java web application?

I'm running a web application in JBoss AS 5. I also have a servlet filter which intercepts all the requests to the server. Now, I want to redirect the users to the login page, if the session has expired. I need to do this 'isSessionExpired()' check in the filter and need to redirect the user accordingly. How do I do it? I'm setting my session time limit in web.xml, as below:

<session-config>     <session-timeout>15</session-timeout> </session-config> 
like image 892
Veera Avatar asked Jun 22 '09 12:06

Veera


People also ask

How do you check session is expired or not in Java?

Use getRequestedSessionId to distinguish between new and existing (valid/expired) sessions, and use isRequestedSessionIdValid to distinguish betwheen valid and new/expired sessions. You can put this code in a Filter.

What happens when session timeout in Java?

Session timeout determines how long the server maintains a session if a user does not explicitly invalidate the session. The default value is 30 minutes. Tune this value according to your application requirements.


1 Answers

You could use a Filter and do the following test:

HttpSession session = request.getSession(false);// don't create if it doesn't exist if(session != null && !session.isNew()) {     chain.doFilter(request, response); } else {     response.sendRedirect("/login.jsp"); } 

The above code is untested.

This isn't the most extensive solution however. You should also test that some domain-specific object or flag is available in the session before assuming that because a session isn't new the user must've logged in. Be paranoid!

like image 148
Alex Avatar answered Sep 16 '22 19:09

Alex