匹配规则 | 使用规则 | 虚拟路径 | 可访问的URL |
---|---|---|---|
完全路径匹配 (精确匹配) |
以/ 开始,不能包含通配符* 。必须完全匹配 |
/myServlet /user/myServlet /product/index.action |
http://localhost:8080/servletDemo/myServlet http://localhost:8080/servletDemo/user/myServlet http://localhost:8080/servletDemo/product/index.action |
目录匹配 |
以/ 字符开头,并以/* 结尾的字符串。用于路径匹配 |
/user/* /* |
http://localhost:8080/servletDemo/user/aaa http://localhost:8080/servletDemo/aa |
扩展名匹配 |
以通配符*. 开头的字符串。用于扩展名匹配 |
*.do *.action *.jsp |
http://localhost:8080/servletDemo/user.do http://localhost:8080/servletDemo/myServlet.action http://localhost:8080/servletDemo/bb.jsp |
缺省匹配(默认匹配) |
映射路径为/ ,表示这个 Servlet 为当前应用的缺省 Servlet 或默认 Servlet,默认处理无法匹配到虚拟路径的请求。 |
/ | 可以匹配任意请求 URL |
/rest/*.do
这种写法是不正确的。
package net.biancheng.www; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class VirtualPathServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=UTF-8"); PrintWriter writer = resp.getWriter(); // 向页面输出 writer.write("本次访问的Servlet是:" + this.getServletName()); writer.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" metadata-complete="false" version="4.0"> <servlet> <servlet-name>MyServlet1</servlet-name> <servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class> </servlet> <servlet> <servlet-name>MyServlet2</servlet-name> <servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class> </servlet> <servlet> <servlet-name>MyServlet3</servlet-name> <servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class> </servlet> <servlet> <servlet-name>MyServlet4</servlet-name> <servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class> </servlet> <servlet> <servlet-name>MyServlet5</servlet-name> <servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class> </servlet> <!-- 完全路径匹配 --> <servlet-mapping> <servlet-name>MyServlet1</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!-- 目录匹配 --> <servlet-mapping> <servlet-name>MyServlet2</servlet-name> <url-pattern>/abc/my/*</url-pattern> </servlet-mapping> <!-- 目录匹配 --> <servlet-mapping> <servlet-name>MyServlet3</servlet-name> <url-pattern>/abc/*</url-pattern> </servlet-mapping> <!-- 扩展名匹配 --> <servlet-mapping> <servlet-name>MyServlet4</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--缺省匹配 --> <servlet-mapping> <servlet-name>MyServlet5</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" metadata-complete="false" version="4.0"> <!--使用tomcat的缺省Servlet访问静态资源或图片 --> <!--需要先将项目内的缺省Servlet删除 --> <!--缺省匹配 --> <!--<servlet-mapping> <servlet-name>MyServlet5</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>--> </web-app>
本文链接:http://task.lmcjl.com/news/13485.html