servlet和路径参数如/xyz/{value}/test,如何在web.xml中映射? [英] Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
问题描述
servlet 是否支持如下网址:
Does servlet support urls as follows:
/xyz/{value}/test
其中 value 可以由文本或数字替换.
where value could be replaced by text or number.
如何在 web.xml 中映射它?
How to map that in the web.xml?
推荐答案
Servlet API 不支持在映射中间使用 URL 模式通配符 *
.它只允许在映射结束时使用通配符 *
像 /prefix/*
或在映射开始时像这样 *.suffix
>.
It's not supported by Servlet API to have the URL pattern wildcard *
in middle of the mapping. It only allows the wildcard *
in the end of the mapping like so /prefix/*
or in the start of the mapping like so *.suffix
.
使用标准允许的 URL 模式语法,最好的办法是将其映射到 /xyz/*
并使用 HttpServletRequest#getPathInfo()
.
With the standard allowed URL pattern syntax your best bet is to map it on /xyz/*
and extract the path information using HttpServletRequest#getPathInfo()
.
所以,给定一个
,这里有一个基本的启动示例,如何提取路径信息、空检查和数组索引省略了边界检查:
So, given an <url-pattern>/xyz/*</url-pattern>
, here's a basic kickoff example how to extract the path information, null checks and array index out of bounds checks omitted:
String pathInfo = request.getPathInfo(); // /{value}/test
String[] pathParts = pathInfo.split("/");
String part1 = pathParts[1]; // {value}
String part2 = pathParts[2]; // test
// ...
如果您希望使用 Apache HTTPD 的 mod_rewrite
进行更细粒度的控制,那么您可以查看 Tuckey 的 URL 重写过滤器 或 自制您自己的 URL 重写过滤器.
If you want more finer grained control like as possible with Apache HTTPD's mod_rewrite
, then you could look at Tuckey's URL rewrite filter or homegrow your own URL rewrite filter.
这篇关于servlet和路径参数如/xyz/{value}/test,如何在web.xml中映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!