How to handle redirect from ajax request (struts)


Web tier: servlets, JSP, Web frameworks: How to handle redirect from ajax request (struts)

  1. How to handle redirect from ajax request (struts) (1 messages)

    When a session-timeout occurs, my webapp redirects to the logon page. When this happens no an AJAX request, the response (login page) is rendered in the element where the "expected" response should go.

    I can't quite figure out a pattern generic enough to handle this situation. I'm currently thinking about creating a 'text/javascript' response, and doing an eval(request.responseText).

    So the responseText would be something similar to window.location.reload()

    My initial issues with this approach are:
    1. I have to 'know' if its an ajax request
    2. making the javascript function redirect in all circumstance (eg could be from an iframe)

    Any suggestions welcome
  2. I have used a filter to capture all requests. The filter is as given below. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; HttpSession session = ((HttpServletRequest)request).getSession(false); String strServletPath=req.getServletPath(); if(strServletPath.indexOf("")!=-1){ //Bypass Login request chain.doFilter(request,response); }else{ if (session != null && !session.isNew()) {// Active Session chain.doFilter(request,response); }else{ // Inactive Session //Ajax request if(req.getParameterMap().get("RequestType")!= null){ if(req.getParameter("RequestType").equalsIgnoreCase("BBLoad")) res.sendError(599,"Session timeout."); }else{ //Not an Ajax request ((HttpServletResponse)response).sendRedirect(loginUrl); } } } } The parameter 'RequestType'was added to the parameter list before sending an AJAX request. request state will be 599 in case of a session timeout then we can redirect back to login page if (oRequest.status == 200) { // valid response } else if(oRequest.status == 599){ var url = "/"; document.forms[0].action=url; document.forms[0].submit(); } I think this will work but implementation need to be done for each AJAX request... If any one have a better solution kindly post your reply...