特定于请求参数的 Java 过滤器 URL 模式

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

Java Filter URL pattern specific to request params

javajakarta-eeservlet-filters

提问by Rohit Desai

We have a situation where we want to use filter for URL's containing some specific request parameters, e.g:

我们有一种情况,我们想对包含一些特定请求参数的 URL 使用过滤器,例如:

http://mydomain.com/?id=78&formtype=simple_form&.......    
http://mydomain.com/?id=788&formtype=special_form&.......    

and so on, idare fetched at run time, I want configure filter in web.xmlonly if formtype=special_form. How should achieve the solution? Can Filter be configured with regex patterns?

等等,id是在运行时获取的,我web.xml只希望在formtype=special_form. 应该如何实现解决?过滤器可以配置正则表达式吗?

回答by stefanglase

As far as I know there is no solution for matching requests to filters by query string directly in web.xml. So you could register the filter in your web.xmlusing init-params to make the filter configurable and set a pattern via void init(FilterConfig filterConfig)in your javax.servlet.Filterimplementation.

据我所知,没有直接在web.xml. 因此,您可以在web.xmlusing init-params 中注册过滤器,以使过滤器可配置并通过void init(FilterConfig filterConfig)在您的javax.servlet.Filter实现中设置模式。

package mypackage;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class MyFilter implements Filter {

    private String pattern;

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // check whether we have a httpServletRequest and a pattern
        if (this.pattern != null && request instanceof HttpServletRequest) {
            // resolve the query string from the httpServletRequest
            String queryString = ((HttpServletRequest) request).getQueryString();
            // check whether a query string exists and matches the given pattern
            if (queryString != null && queryString.matches(pattern)) {
                // TODO do someting special
            }
        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.pattern = filterConfig.getInitParameter("pattern");
    }

}

The configuration would look like this in your web.xml:

您的 web.xml 中的配置如下所示:

<!-- MyFilter -->
<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>mypackage.MyFilter</filter-class>
    <init-param>
        <param-name>pattern</param-name>
        <param-value>{{PATTERN HERE}}</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Further readings:
http://java.sun.com/javaee/5/docs/api/javax/servlet/Filter.html

进一步阅读:http:
//java.sun.com/javaee/5/docs/api/javax/servlet/Filter.html

回答by Paul Ianas

You must parse the URL params in the body of the filter, not in web.xml;

您必须解析过滤器主体中的 URL 参数,而不是 web.xml

I made such a thing, but I used a phase listener, a configuration entry (in a configuration file) that mapped URL params to FQN of classes that handle a particular "action" (by action I mean a URL param from a GET/POST); than, the phase listener would parse all the URL params from each GET/POST and send each of them to a Dispatcher. The Dispatcher's job is to call the right handler (singleton object corresponding to the FQN for that URL param). The handler then just does the specific thing with the URL value he receives.

我做了这样的事情,但我使用了一个阶段侦听器,一个配置条目(在配置文件中)将 URL 参数映射到处理特定“操作”的类的 FQN(通过操作我的意思是来自 GET/POST 的 URL 参数); 然后,阶段侦听器将解析来自每个 GET/POST 的所有 URL 参数,并将它们中的每一个发送到 Dispatcher。Dispatcher 的工作是调用正确的处理程序(对应于该 URL 参数的 FQN 的单个对象)。然后处理程序只用他收到的 URL 值做特定的事情。

The same thing can be made by using a filter instead of phase listener.

同样的事情可以通过使用过滤器而不是相位监听器来完成。