简体   繁体   中英

can I redirect the page without any request to server in java?

If session expires for specific time which is mentioned in web.xml file of my web application, without clicking any link, my app need to redirect to the login page. I tried to use Listener class which implements HttpSessionListener, but I am not able to redirect to new page because in normal listener there in no response object(response.sendRedirect("login.jsp")).

thanks,Jampanna

You cannot do this. At least not so easy as you think.

If the session expires, it means, that there was no request from the client browser during the session timeout time. The server even don't know, if the user closed the browser window or was going to toilet, etc.

The HttpSessionListener is a server side only event, there is no client request. It indicates that there were no client requests for that session in the last 30 minutes (if the session timeout is 30 minutes).

As a consequence you cannot redirect the browser window to another site.

As an alternative you can frequently poll from the client's browser. But that would mean, the server side session will only expire, if the user navigates to another side or closes the browser. To avoid this, you could implement your own session timeout. At each poll request you could determine the time of the last "real" request (means no poll request) and then redirect to the login page and manually close the session.

Another alternative would be to use websockets as a backward channel.

Use Spring Security Framework , it has all the machinery to cope with session end/re-login cases

Alternatively you can use javascript for redirecting to login.jsp, something like

function redirectToLogin(){ document.location = '/login.jsp' }

setTimeout( redirectToLogin, 300 );

this might work only for really simple webapps. For RIA you must check the session state via requesting the server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM