訪問servlet的路徑問題

1、url-pattern的三種配置

  在web.xml配置文件中配置有關Servlet的時候,<url-pattern>標籤是用於配置當前Servlet攔截的路徑,也就是說,客戶端瀏覽器訪問<url-pattern>標籤配置的路徑才能訪問對應Servlet內容。html

關於攔截路徑的配置方式其實有三種方式:java

  1. 徹底路徑匹配:是以「/」開始,路徑中間不能包含通配符「*」,例如:/firstServclet,表示訪問路徑爲http://localhost:8080/08_servlet/firstServlet。
  2. 目錄匹配:是以「/」開始,以「/*」結尾的,例如:/firstServlet/*,表示訪問路徑爲http://localhost:8080/08_servlet/firstServlet路徑下任意內容。
  3. 擴展名匹配:是以「*」開始,不能以「/」開始,以「.xxx」結尾,例如:*.do,表示訪問路徑爲全部擴展名爲「.do」的路徑。

值得注意的問題:web

  1. 在一個<servlet-mapping>標籤中,能夠配置多個<url-pattern>標籤。也就是說,一個Servlet能夠攔截多個不一樣路徑的訪問。
  2. 上述三種配置路徑方式具備優先級:徹底路徑匹配 -> 目錄匹配 -> 擴展名匹配。

下面經過一些測試,來看看路徑配置的三種方式:apache

以下有一些映射關係:瀏覽器

  1. Servlet1 映射到 /abc/*
  2. Servlet2 映射到 /*
  3. Servlet3 映射到 /abc
  4. 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的相關配置,具體內容以下: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>

 

1.2. 相對路徑與絕對路徑

以前咱們開發的Servlet,在客戶端瀏覽器中都是直接在地址欄中輸入路徑來訪問的。若是建立一個頁面來訪問Servlet應該怎麼樣呢?下面咱們來看一看:jsp

  • Web工程中的WebRoot目錄下,建立一個新目錄名爲「html」,而後在該目錄下建立一個新的HTML頁面。

<!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>

  • 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>

什麼是絕對路徑與相對路徑:

  • 絕對路徑:就是不管當前資源在什麼位置,都能經過當前資源訪問到目標資源。
  • 相對路徑:就是判斷當前資源與目標資源的相對位置,找出相對當前資源能夠訪問到目標資源的路徑。
相關文章
相關標籤/搜索