java 适用于所有 webapp 的 tomcat 过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7808544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:31:14  来源:igfitidea点击:

tomcat filter for all webapps

javatomcat

提问by lolo

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. i created it in netbeans, which created 2 separated directories:

例如,它被称为 MyFilter。我在 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? or should i only be in the specific webapp's web.xml?

我需要在哪里找到项目文件?我需要哪些项目文件?我应该编辑 conf/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. when i start it, it fails to load ://localhost:8080/ but successfully loads (and filters) ://localhost:8080/MyFilter

我将过滤器添加到 tomcat/conf/web.xml 中。当我启动它时,它无法加载 ://localhost:8080/ 但成功加载(和过滤器)://localhost:8080/MyFilter

回答by Joachim Sauer

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

根据定义,过滤器只会在定义它的 Web 应用程序中运行。

You could define it in the web.xmlin 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).

你可以在定义它web.xmlconf/Tomcat的目录将它包含在默认情况下(注意,你想也需要使过滤器类可用于所有的Web应用程序,部署到Tomcat的所有Web应用程序可能是通过把它在lib/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.

或者,您可以实现Tomcat Valve,它在概念上类似于过滤器,但可以安装在 Tomcat 中的 Engine、Host 或 Context 上。