首先須要明確幾容易混淆的規則:html
<url-pattern>中配置的項必須與url徹底精確匹配。web
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/user/users.html</url-pattern> <url-pattern>/index.html</url-pattern> <url-pattern>/user/addUser.action</url-pattern> </servlet-mapping>
當在瀏覽器中輸入以下幾種url時,都會被匹配到該servlet
http://localhost:8080/appDemo/user/users.html
http://localhost:8080/appDemo/index.html
http://localhost:8080/appDemo/user/addUser.action正則表達式
注意:apache
http://localhost:8080/appDemo/user/addUser/ 是非法的url,不會被看成http://localhost:8080/appDemo/user/addUser識別瀏覽器
另外上述url後面能夠跟任意的查詢條件,都會被匹配,如app
http://localhost:8080/appDemo/user/addUser?username=Tom&age=23 會被匹配到MyServlet。jsp
以「/」字符開頭,並以「/*」結尾的字符串用於路徑匹配url
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/user/*</url-pattern> </servlet-mapping>
路徑以/user/開始,後面的路徑能夠任意。好比下面的url都會被匹配。
http://localhost:8080/appDemo/user/users.html
http://localhost:8080/appDemo/user/addUser.action
http://localhost:8080/appDemo/user/updateUser.actionlspa
以「*.」開頭的字符串被用於擴展名匹配debug
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.action</url-pattern> </servlet-mapping>
則任何擴展名爲jsp或action的url請求都會匹配,好比下面的url都會被匹配
http://localhost:8080/appDemo/user/users.jsp
http://localhost:8080/appDemo/toHome.action
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
匹配方法只有三種,要麼是路徑匹配(以「/」字符開頭,並以「/*」結尾),要麼是擴展名匹配(以「*.」開頭),要麼是精確匹配,三種匹配方法不能進行組合,不要想固然使用通配符或正則規則。
如<url-pattern>/user/*.action</url-pattern>是非法的
另外注意:<url-pattern>/aa/*/bb</url-pattern>是精確匹配,合法,這裏的*不是通配的含義
Tomcat在%CATALINA_HOME%\conf\web.xml文件中配置了默認的Servlet,配置代碼以下
<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> <servlet-name>jsp</servlet-name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <init-param> <param-name>fork</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>xpoweredBy</param-name> <param-value>false</param-value> </init-param> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- The mappings for the JSP servlet --> <servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.jspx</url-pattern> </servlet-mapping>
映射的URL | 對應的Servlet |
/hello | servlet1 |
/bbs/admin/* | servlet2 |
/bbs/* | servlet3 |
*.jsp | servlet4 |
/ | servlet5 |
實際請求映射的結果
去掉上下文路徑的剩餘路徑 |
處理請求的Servlet |
/hello |
servlet1 |
/bbs/admin/login |
servlet2 |
/bbs/admin/index.jsp |
servlet2 |
/bbs/display |
servlet3 |
/bbs/index.jsp |
servlet3 |
/bbs |
servlet3 |
/index.jsp |
servler4 |
/hello/index.jsp |
servlet4 |
/hello/index.html |
servlet5 |
/news |
servlet5 |