0

I'm a little bit newbie using Java EE Filters and I have a doubt about them.

I want to create a Filter that do some stuff when every web page of my site (JSP) are loaded. For example, if I want to visit index.jsp, the Filter is loaded first and then index.jsp is loaded. The same for concact.jsp, sales.jsp, etc.

For that reason I decided to use this code in web.xml:

<filter-mapping>
    <filter-name>MainFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>

The problem is that the most of my JSPs include inside other auxiliary JSP like navigation bars, footer messages, etc. So, my index.jsp would be like this:

<html>
   <head>
  ... 
   </head>
   <body>
      <%@include file="/WEB-INF/includes/header.jsp"%>
      <%@include file="/WEB-INF/includes/navbar.jsp"%>

      // jsp/html/js stuff here...

      <%@include file="/WEB-INF/includes/footer.jsp"%>
   </body>
</html>

So, if I call index.jsp, the filter is called 4 times (1 for the index.jsp and 3 for the includes).

How could I fix this to call just once to the cookie filter? Any ideas? I'm getting mad with it...

Thanks!

Ommadawn
  • 2,450
  • 3
  • 24
  • 48
  • 2
    Possible duplicate of [Can I exclude some concrete urls from inside ?](https://stackoverflow.com/questions/3125296/can-i-exclude-some-concrete-urls-from-url-pattern-inside-filter-mapping) – vanje Aug 13 '17 at 12:06
  • Thanks for your contribution. Sometimes it's difficult to find the correct topic in the searcher :) – Ommadawn Aug 13 '17 at 15:47

1 Answers1

1

Try to use the next filter config instead:

<filter-mapping>
    <filter-name>MainFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
Simon Sadetsky
  • 544
  • 2
  • 5