在web.xml配置文件中配置有關Servlet的時候,<url-pattern>標籤是用於配置當前Servlet攔截的路徑,也就是說,客戶端瀏覽器訪問<url-pattern>標籤配置的路徑才能訪問對應Servlet內容。html
關於攔截路徑的配置方式其實有三種方式:java
值得注意的問題:web
下面經過一些測試,來看看路徑配置的三種方式:apache
以下有一些映射關係:瀏覽器
問題:服務器
若是客戶端瀏覽器請求的路徑是錯誤時,頁面會顯示404錯誤內容。這是由於全部發布到Tomcat服務器的Web應用程序的web.xml文件都繼承了Tomcat服務器安裝目錄中conf目錄中的web.xml文件。當訪問路徑是錯誤的,或者對應Servlet沒有配置,實際上會執行Tomcat服務器中的web.xml的相關配置,具體內容以下:app
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>
以前咱們開發的Servlet,在客戶端瀏覽器中都是直接在地址欄中輸入路徑來訪問的。若是建立一個頁面來訪問Servlet應該怎麼樣呢?下面咱們來看一看:jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">測試 <html>ui <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> |
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>
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);
}
}
<?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>
訪問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頁面中利用相對路徑訪問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>
什麼是絕對路徑與相對路徑: