一、url-pattern的三種配置
在web.xml配置文件中配置有關Servlet的時候,<url-pattern>標簽是用於配置當前Servlet攔截的路徑,也就是說,客戶端瀏覽器訪問<url-pattern>標簽配置的路徑才能訪問對應Servlet內容。
關於攔截路徑的配置方式其實有三種方式:
- 完全路徑匹配:是以“/”開始,路徑中間不能包含通配符“*”,例如:/firstServclet,表示訪問路徑為http://localhost:8080/08_servlet/firstServlet。
- 目錄匹配:是以“/”開始,以“/*”結尾的,例如:/firstServlet/*,表示訪問路徑為http://localhost:8080/08_servlet/firstServlet路徑下任意內容。
- 擴展名匹配:是以“*”開始,不能以“/”開始,以“.xxx”結尾,例如:*.do,表示訪問路徑為所有擴展名為“.do”的路徑。
值得注意的問題:
- 在一個<servlet-mapping>標簽中,可以配置多個<url-pattern>標簽。也就是說,一個Servlet可以攔截多個不同路徑的訪問。
- 上述三種配置路徑方式具有優先級:完全路徑匹配 -> 目錄匹配 -> 擴展名匹配。
下面通過一些測試,來看看路徑配置的三種方式:
如下有一些映射關系:
- Servlet1 映射到 /abc/*
- Servlet2 映射到 /*
- Servlet3 映射到 /abc
- Servlet4 映射到 *.do
問題:
- 當請求URL為“/abc/a.html”,“/abc/*”和“/*”都匹配,哪個servlet響應?Servlet1
- 當請求URL為“/abc”時,“/abc/*”和“/abc”都匹配,哪個servlet響應?Servlet3
- 當請求URL為“/abc/a.do”時,“/abc/*”和“*.do”都匹配,哪個servlet響應?Servlet1
- 當請求URL為“/a.do”時,“/*”和“*.do”都匹配,哪個servlet響應?Servlet2
- 當請求URL為“/xxx/yyy/a.do”時,“/*”和“*.do”都匹配,哪個servlet響應?Servlet2
如果客戶端瀏覽器請求的路徑是錯誤時,頁面會顯示404錯誤內容。這是因為所有發布到Tomcat服務器的Web應用程序的web.xml文件都繼承了Tomcat服務器安裝目錄中conf目錄中的web.xml文件。當訪問路徑是錯誤的,或者對應Servlet沒有配置,實際上會執行Tomcat服務器中的web.xml的相關配置,具體內容如下:
1 <servlet> 2 <servlet-name>default</servlet-name> 3 4 <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> 5 <init-param> 6 <param-name>debug</param-name> 7 <param-value>0</param-value> 8 </init-param> 9 <init-param> 10 <param-name>listings</param-name> 11 <param-value>true</param-value> 12 </init-param> 13 <load-on-startup>1</load-on-startup> 14 </servlet> 15 <servlet-mapping> 16 <servlet-name>default</servlet-name> 17 <url-pattern>/</url-pattern> 18 </servlet-mapping>
1.2. 相對路徑與絕對路徑
之前我們開發的Servlet,在客戶端瀏覽器中都是直接在地址欄中輸入路徑來訪問的。如果創建一個頁面來訪問Servlet應該怎么樣呢?下面我們來看一看:
- 在Web工程中的WebRoot目錄下,創建一個新目錄名為“html”,然后在該目錄下創建一個新的HTML頁面。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>01.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <h1>相對路徑訪問Servlet</h1><br> <a href="">相對路徑訪問Servlet</a> <h1>絕對路徑訪問Servlet</h1><br> <a href="">絕對路徑訪問Servlet</a> </body> </html> |
- 在Web工程中的WebRoot目錄下,創建一個新的HTML頁面。
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <html> 3 <head> 4 <title>02.html</title> 5 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 6 <meta http-equiv="description" content="this is my page"> 7 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 8 </head> 9 <body> 10 <h1>相對路徑訪問Servlet</h1><br> 11 <a href="">相對路徑訪問Servlet</a> 12 <h1>絕對路徑訪問Servlet</h1><br> 13 <a href="">絕對路徑訪問Servlet</a> 14 </body> 15 </html>
- 在Web工程中創建一個Servlet。
public class PathServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("成功訪問到Servlet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- 配置Web工程的web.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <servlet> <servlet-name>PathServlet</servlet-name> <servlet-class>app.java.servlet.PathServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>PathServlet</servlet-name> <url-pattern>/pathServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
- 將當前Web工程發布到Tomcat服務器,並啟動Tomcat服務器。
- 打開瀏覽器,分別訪問01.html、02.html和PathServlet。
訪問01.html的路徑:http://localhost:8080/08_servlet/html/01.html。
訪問02.html的路徑:http://localhost:8080/08_servlet/02.html。
訪問PathServlet的路徑:http://localhost:8080/08_servlet/pathServlet。
- 根據上述的訪問路徑,可以知道在01.html和02.html頁面中,通過絕對路徑訪問PathServlet是相同的。
- 在01.html和02.html頁面中,通過相對路徑訪問PathServlet是不同的。
在01.html頁面中利用相對路徑訪問PathServlet應該是../pathServlet。原因是pathServlet是在01.html頁面的父級目錄中。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>01.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <h1>相對路徑訪問Servlet</h1><br> <a href="../pathServlet">相對路徑訪問Servlet</a> <h1>絕對路徑訪問Servlet</h1><br><a href="http://localhost:8080/08_servlet/pathServlet">絕對路徑訪問Servlet</a>
</body></html>
² 在01.html頁面中利用相對路徑訪問PathServlet應該是./pathServlet或直接訪問攔截名稱pathServlet。原因是pathServlet與02.html頁面處在同一級別的目錄中。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>02.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <h1>相對路徑訪問Servlet</h1><br> <a href="pathServlet">相對路徑訪問Servlet</a> <h1>絕對路徑訪問Servlet</h1><br> <h1>絕對路徑訪問Servlet</h1><br> <a href="http://localhost:8080/08_servlet/pathServlet"> 絕對路徑訪問Servlet</a> </body> </html>
什么是絕對路徑與相對路徑:
- 絕對路徑:就是無論當前資源在什么位置,都能通過當前資源訪問到目標資源。
- 相對路徑:就是判斷當前資源與目標資源的相對位置,找出相對當前資源可以訪問到目標資源的路徑。