Servlet的配置
生活随笔
收集整理的這篇文章主要介紹了
Servlet的配置
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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> <!-- 最后訪問的就是這個(gè) --></servlet-mapping>其中url-pattern的配置方式
1)完全匹配
訪問的資源與配置的資源完全相同才能訪問的到
2)目錄匹配
/虛擬的目錄…/*
3)擴(kuò)展名匹配
*.擴(kuò)展名
注意:第二種跟第三種不要混用
例如:/wsq/qb/yy/*.wsq 這是錯(cuò)誤的
2,服務(wù)器啟動(dòng)實(shí)例化Servlet配置
Servlet默認(rèn)第一次訪問的時(shí)候創(chuàng)建
當(dāng)servlet的配置時(shí),加上一個(gè)配置<load-on-startup>3</load-on-startup>的時(shí)候,對(duì)象在服務(wù)器啟動(dòng)時(shí)就創(chuàng)建,數(shù)字(正整數(shù))代表優(yōu)先級(jí),越小優(yōu)先級(jí)越高。
3,缺省Servlet
可以將url-pattern配置一個(gè)/,<url-pattern>/</url-pattern>,代表該servlet 是缺省的servlet(當(dāng)你訪問資源地址所以的servlet都不匹配時(shí),缺省的servlet負(fù)責(zé)處理),其實(shí),web應(yīng)用中所有的資源響應(yīng)都是servlet負(fù)責(zé)的,包括靜態(tài)資源
4,歡迎界面
在WEB13這個(gè)項(xiàng)目工程下面的WebContent下創(chuàng)建一個(gè)index.html,當(dāng)?shù)刂吩L問http://localhost:8080/WEB13/,只訪問到項(xiàng)目名稱的時(shí)候,就會(huì)自動(dòng)訪問index.html該頁面。
優(yōu)先級(jí)如下所示:
<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>其實(shí)當(dāng)把項(xiàng)目工程放到TomCat上,訪問的地址為:http://localhost:8080/WEB13/wsq
這里的WEB13是項(xiàng)目的文件名;
這里的wsq是WEB13 ----->WebContent ------>WEB-INF ----->web.xml -----><url-pattern>/wsq</url-pattern>這個(gè)路徑名稱,它找<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>總結(jié)
以上是生活随笔為你收集整理的Servlet的配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 颐和园需要多久时间游玩
- 下一篇: ++i与i++的根本性区别(两个代码对比