views:

172

answers:

1

I have a WEB Application deployed in Tomcat. I would like to intercept all the incoming requests - get or post and perform some task. I want to intercept calls from servlet, from JSP pages etc. So I created one web.xml file which looks like this one -

  <servlet>
    <description></description>
    <display-name>Transformer</display-name>
    <servlet-name>Transformer</servlet-name>
    <servlet-class>com.test.Transformer</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Transformer</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

As we can see, any request will come to this controller servlet called Transformer. Now this servlet usually tries to transform one request from A to B. The problem I am facing is - I am getting into a loop I just want to transform request for url /test.jsp to /abc/test.jsp but the second request /abc/test.jsp is also hitting the Transformer servlet and as a result it is not working as intended. I think I can use Filter but I have too many servlets and JSP pages in the application to put filter everywhere.

+6  A: 

Use a javax.servlet.Filter for intercepting. You can map it to /* and it will intercept everything.

<filter>
    <filter-name>YourFilterName</filter-name>
    <filter-class>com.package.YourFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>YourFilterName</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Bozho
Yes you are correct. I was confused on the usage of the filter. It solved my problem.
Shamik