*
The moose likes Servlets and the fly likes WebFilter Annotation and servletNames attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "WebFilter Annotation and servletNames attribute" Watch "WebFilter Annotation and servletNames attribute" New topic
Author

WebFilter Annotation and servletNames attribute

Murat Balkan
Ranch Hand

Joined: Sep 10, 2002
Posts: 127
Hi all,

I have a problem with a filter deployment. I have a very simple filter that just outputs a message to the console via a System.out.println call.
To test the filter, I created a simple Servlet named TestServlet which actually does nothing in its doGet. TestServlet has been deployed on servlet path /TestServlet under the webcontent (MyProject)
I confirm that I can call the servlet via http://localhost:8080/MyProject/TestServlet without any problems.

My problem is, when I specify the servlet name in WebFilter annotation, tomcat seems to ignore it. (ServletFilter is not triggered.)

@WebFilter(servletNames = { "TestServlet" })
public class TestFiltre implements Filter {}

But when I use urlPatterns attribute, The filter works fine.

@WebFilter(urlPatterns = { "/TestServlet" })


Does Tomcat support servletNames in Filters? Or I have a configuration problem?

Thanks a lot,

Murat
Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24

Murat Balkan wrote:Does Tomcat support servletNames in Filters? Or I have a configuration problem?


i have used filters via eclipse and tomcat. Either you should give servletname or url-pattern in the filter-mapping of the web.xml file.

Ya Tomcat supports servlet names in filters. giving servlet names in filter-mapping is old practise and now they are using url-pattern. For me tomcat 6 supports both type. i need to check in tomcat 7. but surely it should work !!!


Regards,

Vigneswaran.M
Murat Balkan
Ranch Hand

Joined: Sep 10, 2002
Posts: 127
I removed the annotation from the filter and added a web.xml file to the project. I made the necessary changes in web.xml:

<filter>
<filter-name>Filtre1</filter-name>
<filter-class>filtreler.Filtre1</filter-class>
</filter>
<filter-mapping>
<filter-name>Filtre1</filter-name>
<servlet-name>TestServlet</servlet-name>
</filter-mapping>

And it does not work again. TestServlet runs but filter is not triggered. I can see that the init method of the filter runs, so it is loaded by the container.

Right after that, I tried changing the above code with this:

<filter>
<filter-name>Filtre1</filter-name>
<filter-class>filtreler.Filtre1</filter-class>
</filter>
<filter-mapping>
<filter-name>Filtre1</filter-name>
<url-pattern>/TestServlet</url-pattern>
</filter-mapping>

And it works! So it is not about the annotations or web.xml file. It seems Tomcat ignores the <servlet-name> usage.

Any thoughts? Could this be a bug? Has someone test the same on Tomcat 7?

Thanks a lot,

Murat

Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24

Today i ll confirm and will say you. I didnt check it in Tomcat 7
Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24

i am using eclipse indigo. For me web.xml is not visible. i dont know why. In the earlier versions of eclipse web.xml is visible. But i used servlet-name but tomcat responded with an infinite loop. Even for url-pattern too same response. I guess some mismatch in web.xml file. But i cant able to see it. I ll confirm and post you !!!
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

Maybe you don't have a servlet-name for your servlet?...


OCPJP 6, OCEWCD Java EE 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: WebFilter Annotation and servletNames attribute