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

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

问题描述

我正在为学术目的编写一个项目,其中包括编写一个监控 servlet/jsp 响应时间的过滤器.

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

有可能吗?

注意:值得一提的是,我使用 Apache Tomcat 7 作为首选服务器.

谢谢!

麦基

解决方案

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

启动示例:

import org.apache.catalina.valves.ValveBase;公共类 MyValve 扩展 ValveBase {@覆盖public void invoke(Request request, Response response) 抛出 IOException, ServletException {//...getNext().invoke(request, response);}}

server.xml中注册如下:

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

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.

Is it even possible?

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

Thanks!

Mikey

解决方案

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 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);
    }

}

register it as follows in server.xml:

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

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

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