dcsimg
Automatic redirect on session timeout
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mike_Bibby
Posted On:   Wednesday, June 15, 2005 01:35 AM

How can I redirect automatically to login screen if a users session expires. Currently I am setting session timeout to be 30 minutes at the end of 30 minutes inactivity I want to redirect the user to login page again automatically.

Any help is appreciated. Thanks

Re: Automatic redirect on session timeout

Posted By:   Marcos_Luque  
Posted On:   Thursday, June 16, 2005 02:31 PM

if (session.isNew()){



}else{

.

.

.

.

}



or

if (session.isNew()){

response.sendRedirect("/login.jsp");

}else{

.

.

.

.

}

Re: Automatic redirect on session timeout

Posted By:   atul_gupta  
Posted On:   Wednesday, June 15, 2005 01:56 AM

Hi,

You can create a login filter like this.



package webapplication1;


import javax.servlet.Filter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;


/**

*

* @author me

*/

public class LoginFilter implements Filter {


/** Creates a new instance of LoginFilter */


public LoginFilter() {

}

public void destroy() {

}


public void doFilter(javax.servlet.ServletRequest
servletRequest, javax.servlet.ServletResponse
servletResponse, javax.servlet.FilterChain
filterChain) throws java.io.IOException,
javax.servlet.ServletException {

HttpServletRequest hreq = (HttpServletRequest)
servletRequest;

HttpServletResponse hres = (HttpServletResponse)
servletResponse;

HttpSession session = hreq.getSession();


String url = hreq.getPathTranslated();

boolean loginPage = url.endsWith("Login.jsp");
// boolean isJsp = url.endsWith(".jsp");



/** Dont filter login.jsp because otherwise an endless loop.

* & only filter .jsp otherwise it will filter all images etc as well.

*

*/

if (!loginPage) {


/* There is no User attribute so redirect to login page */

if(session.getAttribute("USER_NAME") == null) {

// logger.info("Session expired, redirecting to login page");

/* Set a message so that login page can pick it up and display it */

session.setAttribute("LOGIN_MESSAGE", "Please signon on this page because you did not signon or your session is timed out.");

hres.sendRedirect("./Login.jsp");

return;

}

}

/* deliver request to next filter */

filterChain.doFilter(servletRequest, servletResponse);



}


public void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException {

}



}




and add this in web.xml after parameter section







Login Filter

webapplication1.LoginFilter





Login Filter

/faces/*






Hope this help.
About | Sitemap | Contact