How to not filter only a folder in JAVA - Filter

Asked

Viewed 701 times

3

I’m 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 I just want the pages .JSP fall into the filter, but I cannot place 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 has not been deployed.
Check the server log for more details.

In short, inside the folder /sistema/ I have another folder that nay I want you to go through Filter.

1 answer

2


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

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

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

Other workaround is map by folder and then put a condition, applying the processing desired 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 which acts as an extension of the original API. Virtually every web framework will possess some kind of API to put a Interceptor or filter more personalized.

  • 1

    the mapping part in another folder worked. But keep looking on the net and I found the way to handle it manually in Filter itself. I thought it was nice and helpful. Follow the link http://stackoverflow.com/questions/3125296/can-i-exclude-some-concrete-urls-from-url-pattern-inside-filter-mapping. E thanks for the help. Tks.

  • 1

    @lionbtt This link is an implementation of workaround proposed in the answer. But instead of using the method endsWith to check the extension instead of the path it uses startsWith to check the folder.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.