我的目标是使用以下模式的所有网址到达相同的servlet servlet1:
myserver.com/path1/*
其中*可以是“x”,“x / xx”,“x / xx / x.cfg”等。 最后我只想在正则表达式[0-9a-zA-Z] +。(cfg | xml | htm)之后处理网址,但我在servlet中这样做很好。 servlet解析原始URL并动态构建请求的配置文件。
当我有以下映射时
<servlet-name>servlet1</servlet-name>
<url-pattern>/path1</url-pattern>
如预期的那样,url myserver.com/path1 会导致servlet1 当我使用下面的映射看起来像是对我有意义的映射时:
<servlet-name>servlet1</servlet-name>
<url-pattern>/path1/*</url-pattern>
我仍然可以使用url myserver.com/path1 / 访问servlet 1.
我无法通过url myserver.com/path1/path2 <访问servlet / i>,这个网址实际上让我回到欢迎文件条目。
实际上,无论我输入什么样的模式,任何带有第二条路径的东西都会让我回到welcome-files条目。对于第二条路径,我的意思是myserver.com/path1/*其中*超过0个字符。
我目前看到的唯一选择是让welcome-files条目处理我的请求(丑陋)或使用
.CFG .XML 的* .htm
使得像myserver.com/XXX.cfg这样的网址有效(myserver.com/X/XXX.cfg不起作用)。这也是丑陋的,但会干扰该webapp的未来发展。
如何使映射按照我希望的方式工作?
答案 0 :(得分:0)
如果大多数时间都调用此路径Filter
并将逻辑放在那里,请尝试/path1
。
您可以完全控制此过滤器。您可以在web.xml中定义以逗号分隔的跳过URL。
我将此Filter用于压缩逻辑。
的web.xml:
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.x.y.z.servlet.MyFilter</filter-class>
<init-param>
<param-name>skipEqualsURIs</param-name>
<param-value></param-value>
</init-param>
<init-param>
<param-name>skipStartsWithURIs</param-name>
<param-value></param-value>
</init-param>
<init-param>
<param-name>skipEndsWithURIs</param-name>
<param-value></param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
MyFilter.java:
public class MyFilter implements Filter {
/** The skip equals ur is set. */
private Set<String> skipEqualsURIsSet = new HashSet<String>();
/** The skip starts with ur is set. */
private Set<String> skipStartsWithURIsSet = new HashSet<String>();
/** The skip ends with ur is set. */
private Set<String> skipEndsWithURIsSet = new HashSet<String>();
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String skipEqualsURIs = filterConfig.getInitParameter("skipEqualsURIs");
if (skipEqualsURIs != null && skipEqualsURIs.trim().length() > 0) {
for (String skipURI : skipEqualsURIs.split(",")) {
skipEqualsURIsSet.add(skipURI);
}
}
String skipStartsWithURIs = filterConfig.getInitParameter("skipStartsWithURIs");
if (skipStartsWithURIs != null && skipStartsWithURIs.trim().length() > 0) {
for (String skipURI : skipStartsWithURIs.split(",")) {
skipStartsWithURIsSet.add(skipURI);
}
}
String skipEndsWithURIs = filterConfig.getInitParameter("skipEndsWithURIs");
if (skipEndsWithURIs != null && skipEndsWithURIs.trim().length() > 0) {
for (String skipURI : skipEndsWithURIs.split(",")) {
skipEndsWithURIsSet.add(skipURI);
}
}
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws ServletException, IOException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String uri = httpServletRequest.getRequestURI();
if (isSkip(uri)) {
// do what you want to do based on skip logic
// filterChain.doFilter(servletRequest, servletResponse);
} else {
// do what you want to do based on skip logic
// filterChain.doFilter(servletRequest, servletResponse);
}
}
private boolean isSkip(String uri) {
boolean skip = skipEqualsURIsSet.contains(uri);
if (!skip) {
for (String skipURI : skipStartsWithURIsSet) {
if (uri.startsWith(skipURI)) {
skip = true;
break;
}
}
}
if (!skip) {
for (String skipURI : skipEndsWithURIsSet) {
if (uri.endsWith(skipURI)) {
skip = true;
break;
}
}
}
return skip;
}
@Override
public void destroy() {
}
}