为 servlet 映射指定否定路径匹配 [英] Specify negative path match for servlet mapping
本文介绍了为 servlet 映射指定否定路径匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 web.xml
中指定负映射?例如,我想为所有请求设置一个过滤器,除了那些匹配 '/public/*
' 的请求.
Is there a way to specify negative mappings in web.xml
? For example, I want to set a filter for ALL requests EXCEPT those matching '/public/*
'.
推荐答案
不,这不可能.您必须在 doFilter()
方法中匹配自己的 URL 模式.将过滤器映射到 /*
并执行以下工作:
No, that's not possible. You'd have to do the URL pattern matching yourself inside the doFilter()
method. Map the filter on /*
and do the following job:
HttpServletRequest req = (HttpServletRequest) request;
if (req.getRequestURI().startsWith("/public/")) {
chain.doFilter(request, response);
return;
}
// ...
或者当实际存在上下文路径时:
or when there's actually a context path:
HttpServletRequest req = (HttpServletRequest) request;
if (req.getRequestURI().startsWith(req.getContextPath() + "/public/")) {
chain.doFilter(request, response);
return;
}
// ...
这篇关于为 servlet 映射指定否定路径匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文