Tuesday, February 2, 2010

Track Session out in DWR

Create a filter class,
package com.jijo;
import java.lang.reflect.Method;
import org.directwebremoting.AjaxFilter;
import org.directwebremoting.AjaxFilterChain;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.extend.LoginRequiredException;


public class DwrFilter implements AjaxFilter {


public Object doFilter(Object obj, Method method, Object[] params, AjaxFilterChain chain) throws Exception {

//Check if session has timedout/invalidated
String username = WebContextFactory.get().getSession().getAttribute("username");
if (username != null) {
}
else{
throw new LoginRequiredException( "sessionout" );
//sessionout is a message,access this message in the jsp or html
}

return chain.doFilter( obj, method, params );
}
}

configure this filter class in your bean tage inside the
dwr.xml


filter tag
eg:
<filter class="com.jijo.DwrFilter" >


Also in your dwr.xml file configure,


covert tag starts

<convert match="org.directwebremoting.extend.LoginRequiredException" converter="bean" />

Then in your jsp page inside the javascript tag,
first include the
/dwr/engine.js and also the

//start javascript tag
dwr.engine.setErrorHandler(errorHandler);
function errorHandler(message, exception){

//forwrd to login page
if(message == 'sessionout'){ //'sessionout'- is message from the filter class you configured
window.location = "login.jsp"
}
}
//end of javascript tag