How to not only filter a folder in JAVA - Filter

3

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

I made the mapping in 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 .JSP pages to drop into the filter, but I can not put 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 has not been deployed.   Check the server log for more details.

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

    
asked by anonymous 10.04.2014 / 18:50

1 answer

2

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

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

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

Another workaround is mapping by the folder and then placing a condition, applying the desired processing only of the requested resource extension for .jsp .

More complex but more powerful and flexible solutions include using a framework such as Google Guice, which has a Servlets Filter API that works as an extension of the original API. Virtually every web framework will have some kind of API to put a more personalized interceptor or filter .

    
10.04.2014 / 19:50