首先用MyEclipse創建一個web Project(工程名起為TestServletProject),新建一個Servlet(這里servlet的名字起TestServlet),將請求的servlet映射名稱設為/TestServlet,(具體步驟可以查看tomcat上servlet程序的配置與處理servlet請求過程)。並在TestServlet的doGet方法中在控制台打印一句“this is TestServlet”
jxf.servlet.TestServlet.java
1 package jxf.servlet; 2 import java.io.IOException; 3 import java.util.Date; 4 import java.io.PrintWriter; 5 import java.text.SimpleDateFormat; 6
7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 public class TestServlet extends HttpServlet { 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 response.getWriter().write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 15 System.out.println("this is TestServlet"); 16 } 17 }
此時打開工程的web.xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
3 <display-name></display-name>
4 <servlet>
5 <description>This is the description of my J2EE component</description>
6 <display-name>This is the display name of my J2EE component</display-name>
7 <!--3、 servlet的內部名稱-->
8 <servlet-name>TestServlet</servlet-name>
9 <!--4、 servlet的類全名: 包名+簡單類名 -->
10 <servlet-class>jxf.servlet.TestServlet</servlet-class>
11 </servlet>
12 <servlet-mapping>
13 <!--2、 servlet的映射內部名稱,通過他可以找到上面的servlet的內部名稱-->
14 <servlet-name>TestServlet</servlet-name>
15 <!--1、 請求servlet的映射路徑-->
16 <url-pattern>/TestServlet</url-pattern>
17 </servlet-mapping>
18 <welcome-file-list>
19 <welcome-file>index.jsp</welcome-file>
20 </welcome-file-list>
21 </web-app>
將項目部署(在tomcat服務器)好后在瀏覽器中輸入http://localhost:8081/TestServletProject/TestServlet(我的端口為8081),這時候服務器就會先找到TestServletProject工程下的web.xml,然后尋找這一次請求的映射路徑/TestServlet,如上圖綠色注釋部分 根據映射/TestServlet找到注釋1,然后找到同級servlet的映射內部名稱注釋2,在根據映射的內部名稱找到servlet的內部名稱注釋3,最后找到同級的servlet的具體類名注釋4,然后服務器在根據反射執行這個類的doGet方法...詳細的Servlet請求的整個生命周期就不在這里討論了。
最終在控制台輸出this is TestServlet,並且頁面上也顯示出當前系統時間,說明該Servlet被請求並執行了。
本文主要介紹 請求servlet的映射路徑 的寫法:
上面的例子就是一種寫法,為精確匹配
如:
/first http://localhost:8080/ProjectName/first
/xxx/demoServlet http://localhost:8080/ProjectName/xxx/demoServlet
還有一種寫法是模糊匹配如:
/* http://localhost:8080/ProjectName/任意路徑
/test/* http://localhost:8080/ProjectName/test/任意路徑
*.后綴名 http://localhost:8080/ProjectName/任意路徑.do 如:*.do *.action *.html(偽靜態)
假如將上面的配置文件
<url-pattern>/TestServlet</url-pattern>改為<url-pattern>/*</url-pattern>
在瀏覽器地址欄中輸入http://localhost:8080/ProjectName/xxxx(任意的請求映射路徑/xxxx)一樣可以請求到TestServlet.
注意:
1)url-pattern(請求servlet的映射路徑)要么以 / 開頭,要么以*開頭。 例如, 只寫test是非法路徑。
2)不能同時使用兩種模糊匹配,例如 /test/*.do是非法路徑
3)當有輸入的URL有多個servlet同時被匹配的情況下:
3.1 精確匹配優先。(長的最像優先被匹配,這里就做不驗證了)
3.2 以后綴名結尾的模糊url-pattern優先級最低
4)雖然能夠以/開頭(/和/*兩種寫法是等價的),但是不推薦這種寫法。為什么?
注意4解答:
當上面的配置文件<url-pattern>/TestServlet</url-pattern>改為<url-pattern>/*</url-pattern> 以后,我們想要訪問項目中默認的靜態文件index.html(創建項目后就自動有了),在瀏覽器中輸入http://localhost:8080/ProjectName/index.html,發現請求的結果也是頁面輸出當前系統時間,控制台也輸出this is TestServlet,可見其實最終訪問到的是/TestServlet。因為 “請求servlet的映射路徑”已經匹配到,服務器就當成是請求該servlet了。所以這里最好不要只寫/或/*,因為項目中的其他靜態資源都將無法訪問到。
可以查看 tomcat根目錄/conf/web.xml ,有下面一段代碼
<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>
可以得知,/是servlet中預定義好的一個映射路徑:servlet的缺省映射路徑(<url-pattern>/</url-pattern>)是在tomcat服務器內置的一個映射路徑。該路徑對應的是一個DefaultServlet(缺省Servlet)。這個缺省的Servlet的作用是用於解析web應用的靜態資源文件。
並且通過此我們還可以得出一個結論:先找動態資源,當動態資源不存在的時候,再找靜態資源