简体   繁体   English

所有webapps的tomcat过滤器

[英]tomcat filter for all webapps

Problem: i want to do filter for all webapps. 问题:我想对所有webapps进行过滤。

I created a filter for monitoring requests to the apache tomcat server. 我创建了一个过滤器来监视对apache tomcat服务器的请求。

for the sake of example, it's called MyFilter. 为了举例,它被称为MyFilter。 i created it in netbeans, which created 2 separated directories: 我在netbeans中创建了它,它创建了2个独立的目录:

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 同样当我从netbeans执行它时,它部署它并将我发送到http:// localhost:8080 / MyFilter

whenever i view a page under /MyFilter/somepath - it runs the filter. 每当我在/ MyFilter / somepath下查看页面时 - 它都会运行过滤器。

i want the filter to run on all paths, and not only for the webapp 我希望过滤器在所有路径上运行,而不仅仅是对于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? 我应该编辑conf / web.xml并添加过滤器吗? or should i only be in the specific webapp's web.xml? 或者我应该只在特定的webapp的web.xml中?

EDIT: 编辑:

i copied the generated .war file into tomcat/webapps 我将生成的.war文件复制到tomcat / webapps中

when i start tomcat, it creates a directory (tomcat/webapps/MyFilter/) 当我启动tomcat时,它会创建一个目录(tomcat / webapps / MyFilter /)

i added the filter into tomcat/conf/web.xml. 我将过滤器添加到tomcat / conf / web.xml中。 when i start it, it fails to load ://localhost:8080/ but successfully loads (and filters) ://localhost:8080/MyFilter 当我启动它时,它无法加载:// localhost:8080 /但成功加载(和过滤器):// localhost:8080 / MyFilter

A filter will by definition only ever run within the web application it is defined in. 根据定义,过滤器只能在其定义的Web应用程序中运行。

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). 您可以在Tomcat的conf/目录中的web.xml中定义它,以便将它包含在默认情况下部署到该Tomcat的所有Web应用程序中(请注意,您还需要使过滤器类可用于所有Web应用程序,可能把它放在Tomcat的lib/目录中)。

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. 或者,您可以实现Tomcat Valve ,它在概念上类似于过滤器,但可以安装在Tomcat中的Engine,Host或Context上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM