Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tomcat filter for all webapps

Tags:

java

tomcat

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

like image 354
lolo Avatar asked Oct 18 '11 14:10

lolo


People also ask

What is webapps folder in Tomcat?

The webapps directory is where deployed applications reside in Tomcat. The webapps directory is the default deployment location, but this can be configured with the appBase attribute on the <Host> element.

What is the purpose of Tomcat filter?

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method.

How do I enable CORS in Tomcat 9?

Tomcat Web Server Config To enable CORS support we have to use CORS Filter. If you want to enable CORS for all webapps, add the filter into $CATALINA_BASE/conf/web. xml. If you want to enable them only for the MOTECH application, add the filter into $CATALINA_BASE/webapps/motech-platform-server/WEB-INF/web.

What is HttpHeaderSecurityFilter?

Class HttpHeaderSecurityFilterProvides a single configuration point for security measures that required the addition of one or more HTTP headers to the response.


1 Answers

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.

like image 147
Joachim Sauer Avatar answered Sep 29 '22 09:09

Joachim Sauer