您将有两个部分需要担心。
- 您的pathSpec
WEB-INF/web.xml
- Servlet中的HttpServletRequest.getPathInfo()。
pathSpec
在您中,
WEB-INF/web.xml您必须声明Servlet和url模式(也称为pathSpec)。
例:
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false" version="3.0"> <display-name>Example WebApp</display-name> <servlet> <servlet-name>clientServlet</servlet-name> <servlet-class>com.mycompany.ClientServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>clientServlet</servlet-name> <url-pattern>/client/*</url-pattern> </servlet-mapping></web-app>
这将
com.mycompany.ClientServlet在名称上设置为类的servlet,
clientServlet然后
/client/*为传入的请求URL
指定url-pattern 。
/*url-pattern末尾的多余
/client/字符可以接受以开头的任何传入模式,这对于pathInfo部分很重要。
pathInfo
接下来,我们进入Servlet实现。
在ClientServlet上的doGet(HttpServletRequest req,HttpServletResponse
resp)实现中,您应该访问req.getPathInfo()值,该值将接收URL
/client模式后面的请求URL部分。
例:
Request URL Path Info---------------- ------------/client///client/hi /hi/client/world/ /world//client/a/b/c /a/b/c
此时,您可以针对“路径信息”中的信息执行任何逻辑操作



