标准Servlet API不支持此功能。你可能想要为此使用改写URL过滤器(例如Tuckey的过滤器)(与Apache HTTPD的过滤器非常相似mod_rewrite
),或者doFilter()在Filter
侦听的方法中添加一个检查/*
。
String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath/")) {
chain.doFilter(request, response); // Just continue chain.
} else {
// Do your business stuff here for all paths other than /specialpath.
}
如有必要,可以将要忽略的路径指定为init-param过滤器的,以便web.xml无论如何都可以对其进行控制。你可以按以下方式在过滤器中获取它:
private String pathToBeIgnored;
public void init(FilterConfig config) {
pathToBeIgnored = config.getInitParameter("pathToBeIgnored");
}
如果过滤器是第三方API的一部分,因此你无法对其进行修改,则将其映射到更具体的url-pattern,例如/otherfilterpath/*
,创建一个新过滤器,/*
并转发到与第三方过滤器匹配的路径。
String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath/")) {
chain.doFilter(request, response); // Just continue chain.
} else {
request.getRequestDispatcher("/otherfilterpath" + path).forward(request, response);
}
为避免此过滤器在无限循环中调用自身,你需要让其REQUEST仅在(第三方)过滤器上侦听(调度)FORWARD。