Intercepting the entire request of one WEBAPP using Java EE

I have a WEB application deployed to Tomcat. I would like to intercept all incoming requests - receive or send and complete some task. I want to intercept calls from a servlet, from JSP pages, etc. So I created one web.xml file that 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 go to this controller servlet called Transformer. Now this servlet usually tries to convert one request from A to B. The problem I am facing is what I get in the loop. I just want to convert the request for url / test.jsp to /abc/test.jsp, but the second request /abc/test.jsp also gets into the Transformer servlet, and as a result, it does not work properly. I think I can use Filter, but I have too many servlets and JSP pages in the application to put the filter everywhere.

+3
source share
1 answer

Use javax.servlet.Filterto intercept. You can match it with /*and 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>
+8
source

Source: https://habr.com/ru/post/1728515/


All Articles