How not to filter only one folder in JAVA-Filter

I am using a class inherited from Filter in Java to do login control.

I did the mapping on Web.xml as follows:

<filter>
    <filter-name>ValidacaoLoginFilter</filter-name>
    <filter-class>br.com.dgtbr.configuracao.ValidacaoLoginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ValidacaoLoginFilter</filter-name>
    <url-pattern>/sistema/*</url-pattern>
</filter-mapping>

My problem is that I want only the pages .JSP to fall into the filter, but I can't put the url-pattern as follows:

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

The following error occurs:

Deployment is in progress...
deploy?config = file%3A % 2FC%3A % 2fusers%2fleonardo%2fappdata%2flocal%2ftemp%2fcontext4308821743855259488.xml & path= / DGTBr.
FAIL-Deployed application at context path ... but context failed to start
......nbproject \ build-impl.xml: 1163: the module was not deployed.
Check the server log for more details.

In short, inside the folder /sistema/ I have another folder that does not I want it to go through the Filter.

Author: brasofilo, 2014-04-10

1 answers

The java API does not allow you to specify this type of pattern. Unfortunately, there are only three ways: by the exact path, path with a * at the end, extension.

Additionally, you can apply the filter to a Servlet using the <servlet-name> tag instead of the <url-pattern> tag.

The simplest and most straightforward way to get around the problem is to put all the .jsp files in a separate folder and map by folder.

Another workaround is to map through the folder and then put a condition, applying the desired processing only of the requested resource extension is .jsp.

More complex, yet more powerful and flexible solutions include using a framework like Google Guice, which has a filter API for Servlet that functions as an extension of the original API. Virtually every web framework will have some kind of API to put a more custom interceptor or filter.
 2
Author: utluiz, 2014-04-10 17:50:00