0

I'm really confused about the WebFilter annotation with Dynamic Web Modules >= 3.0. I read this article ( https://www.concretepage.com/java-ee/jsp-servlet/how-to-use-filter-in-servlet-3-with-webfilter-annotation ) telling that @WebFilter cannot be used without web.xml because @WebFilter does not define order, it only reduces the configuration stored in web.xml.

I was not able to create a web.xml without the url patterns. E.g. this is invalid:

<filter-mapping>
    <filter-name>OAuthSecurityFilter</filter-name>
</filter-mapping>
<filter-mapping>
    <filter-name>GuiceFilter</filter-name>
</filter-mapping>  

It is invalid because in addition to filter-name, one has to provide at least a server name or an url mapping. But in that case, I wonder what it means if I also specify the value to the annotation:

@WebFilter(value="/*", filterName="OAuthSecurityFilter")
public class OAuthSecurityFilter implements Filter {

You see, if you cannot specify the filter without the url pattern in web.xml, and if you must provide the filter in web.xml, then what is the point in having the "value" in the WebFilter annotation? Will it be ignored? Will it override the url-pattern given in web.xml?

nagylzs
  • 3,954
  • 6
  • 39
  • 70
  • Most of the time, the order doesn't matter, or there is only one filter anyway. In that case, you don't need web.xml. – JB Nizet Dec 25 '17 at 14:00
  • I see, so in that special case, web.xml is not needed. But in general, it is not possible to eliminate web.xml. And it also means that if I'm using web.xml, then I should not give url patterns in the annotation, right? – nagylzs Dec 25 '17 at 14:03
  • I don't know what the behavior would be, but it would be confusing anyway, so no, I wouldn't annotate the filter if it's declared in web.xml. – JB Nizet Dec 25 '17 at 14:11
  • I think you should post this as an answer. – nagylzs Dec 25 '17 at 14:13

0 Answers0