全局Java Servlet过滤器,有可能吗? [英] Global Java Servlet Filter, is it possible?

查看:95
本文介绍了全局Java Servlet过滤器,有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用于学术目的的项目,其中包括其他不相关的内容,包括编写一个监视servlet / jsp响应时间的过滤器。

I'm writing a project for academic purposes which among other irrelevant stuff, includes writing a filter which monitors servlet/jsp response times.

问题是过滤器应该适用于服务器中的每个已部署的Web应用程序,而不仅仅是特定的应用程序,我无法找到有关应用全局过滤器的任何信息。

The thing is that the filter should work on every deployed web application in the server and not only over a specific one, I just couldn't find any information regarding applying "global" filters.

是它甚至可能吗?

注意:
重要的是要提到我使用 Apache Tomcat 7作为首选服务器。

NOTE: It's important to mention that i'm using Apache Tomcat 7 as the server of choice.

谢谢!

Mikey

推荐答案

您可以在Tomcat的公共类路径中提供过滤器并编辑Tomcat自己的 /conf/web.xml 来添加过滤器,但这不会运行在不存在的webapp上下文中(即它不包括所有可能的请求),并且在所有已部署的webapps中都可以覆盖它。更强大的解决方案取决于所使用的servlet容器。对于Tomcat,您需要 Valve组件

You could provide the filter in Tomcat's common classpath and edit Tomcat's own /conf/web.xml to add the filter, but this does not run on non-existing webapp contexts (i.e. it does not cover all possible requests) and it is overrideable in all deployed webapps. The more robust solution depends on the servlet container used. In case of Tomcat, you need the Valve component.

Kickoff示例:

Kickoff example:

import org.apache.catalina.valves.ValveBase;

public class MyValve extends ValveBase {

    @Override
    public void invoke(Request request, Response response) throws IOException, ServletException {
        // ...

        getNext().invoke(request, response);
    }

}

在<$中注册如下c $ c> server.xml :

<Valve className="com.example.MyValve" />

这篇关于全局Java Servlet过滤器,有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆