Search code examples
javafilterwebsphereweblogic

Why Filter behaviour is different in Web logic and WebSphere


I have a Filter, in my case it is SessionFilter, It is having some custom logic inside doFilter() method to validate request, In the process of validating this request I am doing db operation. Its working fine when I deployed in Weblogic environment. But facing problem when I deployed same application in WebSphere environment. As per my requirement doFilter() should execute only for once, It is happening in web logic but not in web sphere. Filter get called two times before processing request and after processing the request in web sphere. I know this is expected behaviour as per servlet api spec. But why it is not happening in Weblogic? After successful validation of request I am allowing request by calling chan.doFilter(). Once it is success it should not call same logic to validate request. But in web sphere it is happening. Why same application behaving differently in both servers?

Hi Following is my filter configuration in web.xml.. 

<filter>
    <filter-name>sessionFilter</filter-name>
    <filter-class>com.abc.filter.SessionFilter</filter-class>     
</filter>
  <filter-mapping>
    <filter-name>sessionFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Following is doFilter() code not exactly. But similar way it goes.

public void doFilter(final ServletRequest req, final ServletResponse res,
            final FilterChain chain) throws ServletException {

  //Do some db operation
if (!success){
response.sendRedirect("/../../login.jsp");
return;
}
chain.doFilter(req, res);
}//doFilter
}

Solution

  • You're using a Filter which is mapped to all urls (/*).

    And in your filter you optionally do a redirect:

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

    Now if your filter is supposed to called for all URLs, then inside it redirecting to a relative URL is a really bad idea. Depending on what URL is called/requested, the relative URL to redirect to will be different. In this case you should use an absolute URL for redirect, e.g. "/pages/login.jsp". This might also explain some discrepancy between different number of calls to your filter.