6
votes

Problem: i want to do filter for all webapps.

I created a filter for monitoring requests to the apache tomcat server.

for the sake of example, it's called MyFilter. i created it in netbeans, which created 2 separated directories:

webpages - contains:
    META-INF
    WEB-INF
    index.jsp (a file which i created for viewing monitored data)

source packages
    my.package/MyFilter

in the same when i execute it from netbeans, it deploys it and sends me to http://localhost:8080/MyFilter

whenever i view a page under /MyFilter/somepath - it runs the filter.

i want the filter to run on all paths, and not only for the webapp

Where do i need to locate the project files? which project files do i need? should i edit conf/web.xml and add the filter? or should i only be in the specific webapp's web.xml?

EDIT:

i copied the generated .war file into tomcat/webapps

when i start tomcat, it creates a directory (tomcat/webapps/MyFilter/)

i added the filter into tomcat/conf/web.xml. when i start it, it fails to load ://localhost:8080/ but successfully loads (and filters) ://localhost:8080/MyFilter

1
@BalusC, im aware of valve, but can it be done with filter?lolo
Yes. Perhaps you just need to fix the filter's URL pattern in Tomcat's web.xml?BalusC
it's set to /* , shouldn't it be for all requests?lolo
Yes. Perhaps your problem is caused because you don't have a webapp on the ROOT at all? Filters runs on deployed webapps only. You asked how to have a global filter for all webapps.BalusC

1 Answers

11
votes

A filter will by definition only ever run within the web application it is defined in.

You could define it in the web.xml in the conf/ directory of Tomcat to have it included in all web applications deployed to that Tomcat by default (note that you'd also need to make the filter class available to all web applications, probably by putting it in the lib/ directory of Tomcat).

Alternatively, you can implement a Tomcat Valve, which is conceptually similar to a filter, but can be installed on an Engine, Host or Context in Tomcat.