特定于请求参数的 Java 过滤器 URL 模式 [英] Java Filter URL pattern specific to request params

查看:35
本文介绍了特定于请求参数的 Java 过滤器 URL 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

据我所知,没有直接在 web.xml 中通过查询字符串将请求匹配到过滤器的解决方案.因此,您可以使用 init-params 在您的 web.xml 中注册过滤器以使过滤器可配置并通过 void init(FilterConfig filterConfig) 在您的 中设置模式javax.servlet.Filter 实现.

package mypackage;导入 java.io.IOException;导入 javax.servlet.Filter;导入 javax.servlet.FilterChain;导入 javax.servlet.FilterConfig;导入 javax.servlet.ServletException;导入 javax.servlet.ServletRequest;导入 javax.servlet.ServletResponse;导入 javax.servlet.http.HttpServletRequest;公共类 MyFilter 实现过滤器 {私有字符串模式;@覆盖公共无效销毁(){//TODO 自动生成的方法存根}@覆盖public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)抛出 IOException,ServletException {//检查我们是否有一个 httpServletRequest 和一个模式if (this.pattern != null && request instanceof HttpServletRequest) {//从 httpServletRequest 解析查询字符串String queryString = ((HttpServletRequest) request).getQueryString();//检查查询字符串是否存在并匹配给定的模式if (queryString != null && queryString.matches(pattern)) {//TODO 做一些特别的事情}}chain.doFilter(请求,响应);}@覆盖public void init(FilterConfig filterConfig) 抛出 ServletException {this.pattern = filterConfig.getInitParameter("pattern");}}

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

<过滤器><filter-name>myFilter</filter-name><filter-class>mypackage.MyFilter</filter-class><初始化参数><param-name>模式</param-name><param-value>{{PATTERN HERE}}</param-value></init-param></过滤器><过滤器映射><filter-name>myFilter</filter-name><url-pattern>/*</url-pattern></过滤器映射>

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

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

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

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

解决方案

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.xml using init-params to make the filter configurable and set a pattern via void init(FilterConfig filterConfig) in your javax.servlet.Filter implementation.

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:

<!-- 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

这篇关于特定于请求参数的 Java 过滤器 URL 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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