1,基本配置
<!-- Servlet類的配置 --><servlet><servlet-name>sq</servlet-name><servlet-class>beyond.servlet.QuickStartServlet</servlet-class></servlet><!-- Servlet的虛擬路徑的配置 --> <servlet-mapping><servlet-name>sq</servlet-name><url-pattern>/beyond</url-pattern> <!-- 最后訪問的就是這個 --></servlet-mapping>
其中url-pattern的配置方式
1)完全匹配
訪問的資源與配置的資源完全相同才能訪問的到
<url-pattern>/beyond</url-pattern>
2)目錄匹配
/虛擬的目錄…/*
<url-pattern>/wsq/qb/yy/*</url-pattern> //只要是訪問的是wsq/qb/yy下的任何資源都可以訪問到
3)擴展名匹配
*.擴展名
<url-pattern>*.wsq</url-pattern>
注意:第二種跟第三種不要混用
例如:/wsq/qb/yy/*.wsq 這是錯誤的
2,服務器啟動實例化Servlet配置
Servlet默認第一次訪問的時候創建
當servlet的配置時,加上一個配置<load-on-startup>3</load-on-startup>
的時候,對象在服務器啟動時就創建,數字(正整數)代表優先級,越小優先級越高。
3,缺省Servlet
可以將url-pattern配置一個/,<url-pattern>/</url-pattern>
,代表該servlet 是缺省的servlet(當你訪問資源地址所以的servlet都不匹配時,缺省的servlet負責處理),其實,web應用中所有的資源響應都是servlet負責的,包括靜態資源
4,歡迎界面
在WEB13這個項目工程下面的WebContent下創建一個index.html
,當地址訪問http://localhost:8080/WEB13/
,只訪問到項目名稱的時候,就會自動訪問index.html
該頁面。
優先級如下所示:
<welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
其實當把項目工程放到TomCat上,訪問的地址為:http://localhost:8080/WEB13/wsq
這里的WEB13是項目的文件名;
這里的wsq是WEB13 ----->WebContent ------>WEB-INF ----->web.xml -----><url-pattern>/wsq</url-pattern>
這個路徑名稱,它找<servlet-name>sq</servlet-name>
,
然后再找
<servlet-class>beyond.servlet.QuickStartServlet</servlet-class>
最后找到該包。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>WEB13</display-name><!-- Servlet類的配置 --><servlet><servlet-name>sq</servlet-name><servlet-class>beyond.servlet.QuickStartServlet</servlet-class><init-param><param-name>url</param-name><param-value>beyondsq</param-value></init-param><load-on-startup>3</load-on-startup></servlet><!-- Servlet的虛擬路徑的配置 --> <servlet-mapping><servlet-name>sq</servlet-name><url-pattern>/wsq</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>