java 如何以编程方式添加 servlet 过滤器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7904577/
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:55:09  来源:igfitidea点击:

How can I add a servlet filter programmatically?

javaservletsservlet-filters

提问by Zeemee

Although I've seen many similar questions, I didn't find a clear answer. Using Servlet Spec 2.5, is it possible to add servlet filters and mappings programmatically? The preferred location would be in a Servlet.init() or in ServletContextListener.contextInitialized().

虽然我看过很多类似的问题,但我没有找到明确的答案。使用 Servlet Spec 2.5,是否可以以编程方式添加 servlet 过滤器和映射?首选位置在 Servlet.init() 或 ServletContextListener.contextInitialized() 中。

回答by BalusC

No, not by the standard Servlet 2.5 API. This was introducedin Servlet 3.0. Your best bet is to create a single filter and reinvent the chain of responsibility pattern yourself. An alternative is to grab container specific classes from under the covers and then add the filter by its API. How exactly to do that depends on the target container (and it would also make your code tight coupled to the container in question).

不,不是标准的 Servlet 2.5 API。这是在 Servlet 3.0中引入的。最好的办法是创建一个单一的过滤器并自己重新发明责任链模式。另一种方法是从幕后获取容器特定的类,然后通过其 API 添加过滤器。具体如何执行取决于目标容器(这也会使您的代码与相关容器紧密耦合)。

See also:

也可以看看:



Update:as requested by comment, here's an example in flavor of a ServletContextListenerhow you could add filters programmatically during webapp's startup using Tomcat 6 specific APIs:

更新:根据评论的要求,这里有一个示例,ServletContextListener说明如何使用 Tomcat 6 特定 API 在 webapp 启动期间以编程方式添加过滤器:

package com.example;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.catalina.Container;
import org.apache.catalina.ServerFactory;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.deploy.FilterDef;
import org.apache.catalina.deploy.FilterMap;

public class Tomcat6FilterConfigurator implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        StandardEngine engine = (StandardEngine) ServerFactory.getServer().findService("Catalina").getContainer();
        Container container = engine.findChild(engine.getDefaultHost());
        StandardContext context = (StandardContext) container.findChild(event.getServletContext().getContextPath());

        FilterDef filter1definition = new FilterDef();
        filter1definition.setFilterName(Filter1.class.getSimpleName());
        filter1definition.setFilterClass(Filter1.class.getName());
        context.addFilterDef(filter1definition);

        FilterMap filter1mapping = new FilterMap();
        filter1mapping.setFilterName(Filter1.class.getSimpleName());
        filter1mapping.addURLPattern("/*");
        context.addFilterMap(filter1mapping);

        // ...
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // TODO Auto-generated method stub

    }

}

Register this listener as follows in web.xml:

在以下位置注册此侦听器web.xml

<listener>
    <listener-class>com.example.Tomcat6FilterConfigurator</listener-class>
</listener>

Once again, keep in mind that this does notwork on containers of other make/version, even not on Tomcat 7.0.

再次,要记住,这样做工作对其他品牌/版容器,甚至没有在Tomcat 7.0。