Featured Post

The great debacle of healthcare.gov

This is the first time in history when the president of the United States of America, or probably for any head of state around the world,...

Monday, October 29, 2007

Handle session time out issue in the Ajax application

If you invoke any serverside program in an Ajax call it doesn't refresh the entire page with the resoponse. So in the webapplication using ajax call needs special treatment to track the session time out and refresh the entire page to forward to login page.

Following is one special scenerio where an j2ee application using Struts framework implements the session time out issue.

1. Check if the session is valid for the user

2. If the session is timed-out, return to a particular action mapping forward (e.g. 'session-time-out')

if(session.isNew()) {
return mapping.findForward("session-time-out");
}


3. In the struts-config.xml, forward the session-time-out to a special jsp page (e.g. ajaxSessionTimeOut.jsp) which will contain a particular value that represents the session time out

<action>
<forward="session-time-out" path="/ajaxSessionTimeOut.jsp"/>
</action<

4. In the call back object/method of the ajax call, parse the value from the responseXML. If the parsed value is the value set in the forwarded page (i.e. ajaxSesssionTimeOut.jsp), set the action of the form to the login.do (the action mapping for the login in the application) and submit it

document.forms[0].action="/login.do";
document.forms[0].submit();


The similar technique, shown above, can be used in any other web applicatoin.

No comments: