什么servlet?
Servlet架構,在軟件里面位置如下:
Tomcat:
很多人用過Tomcat,但是不甚其解,市場上有許多 Web 服務器支持 Servlet,Tomcat是其中應用得較多的一款,
Apache Tomcat 是一款 Java Servlet 和 JavaServer Pages 技術的開源軟件實現,可以作為測試 Servlet 的獨立服務器,而且可以集成到 Apache Web 服務器。
這里不做贅述,關於Tomcat具體可見:http://www.runoob.com/servlet/servlet-environment-setup.html
生命周期:
( 右圖來自http://www.runoob.com/servlet/servlet-life-cycle.html,侵刪)
方法說明:
init():
一開始創建,只創建一次,可以簡單地創建或加載一些數據,這些數據將被用於 Servlet 的整個生命周期。
1 public void init() throws ServletException { 2 // 初始化代碼... 3 }
service() :這一層不用管,只需要重寫doGet和doPost方法
service() 方法檢查 HTTP 請求類型(GET、POST、PUT、DELETE 等),並在適當的時候調用 doGet、doPost、doPut,doDelete 等方法。
下面是doGet方法和doPost方法的代碼:
//全部根據傳入的參數來執行響應操作,一般是get接受服務器請求,post對請求發出響應 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet 代碼 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet 代碼 }
destroy() :
destroy() 方法和init()方法一樣,被調用一次,在 Servlet 生命周期結束時被調用。destroy() 方法可以讓您的 Servlet 關閉數據庫連接、停止后台線程、把 Cookie 列表或點擊計數器寫入到磁盤,並執行其他類似的清理活動。在調用 destroy() 方法之后,servlet 對象被標記為垃圾回收。
Servlet 部署:
這里是很關鍵的位置,因為你每寫完一個Servlet都需要在配置文件里面部署一下,記住過程當然簡單,但是還是弄清楚為什么是這樣還是很有必要!
下面以一個案例為例:
如圖,寫了一個CoreServlet實例。在里面實現了doGet()和Post()方法。現在要將其部署在web.xml當中,下面是沒有寫入的。(這個是動態web工程,可以在Eclipse里面直接生成)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>WeixinDFF</display-name> <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>
現在要寫入內容:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>WeixinDFF</display-name> <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> <servlet> <servlet-name>coreServlet</servlet-name> <servlet-class>com.dff.weixin.servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>coreServlet</servlet-name> <url-pattern>/coreServlet</url-pattern> </servlet-mapping> </web-app>
ServletConfig獲取配置參數的方法和ServletContext獲取配置參數的方法完全一樣,只是ServletConfig是取得當前Servlet的配置參數,而ServletContext是獲取整個Web應用的配置參數。使用<init-param>子元素將初始化參數名和參數值傳遞給Servlet,訪問Servlet配置參數通過ServletConfig對象來完成。
參數說明:
1.<servlet>必須含有<servlet-name>和<servlet-class>,或者<servlet-name>和<jsp-file>。
<servlet-name>用來定義servlet的名稱,該名稱在整個應用中必須是惟一的
<servlet-class>用來指定servlet的完全限定的名稱。這里就是指包名。
<jsp-file>用來指定應用中JSP文件的完整路徑。這個完整路徑必須由/開始。
2.<servlet-mapping>要含有<servlet-name>和<url-pattern>
<url-pattern>:指定相對於Servlet的URL的路徑。該路徑相對於web應用程序上下文的根路徑。<servlet-mapping>將URL模式映射到某個Servlet,即該Servlet處理的URL。
其余參數:
加載過程:
容器的Context對象對請求路徑(URL)做出處理,去掉請求URL的上下文路徑后,按路徑映射規則和Servlet映射路徑(<url- pattern>)做匹配,如果匹配成功,則調用這個Servlet處理請求。
擴展:DispatcherServlet在web.xml中的配置:
<!-- Spring view分發器 對所有的請求都由business對應的類來控制轉發 --> <servlet> <servlet-name>business</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>publishContext</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<load-on-startup>
如果load-on-startup元素存在,而且也指定了jsp-file元素,則JSP文件會被重新編譯成Servlet,同時產生的Servlet也被載入內存。<load-on-startup>的內容可以為空,或者是一個整數。這個值表示由Web容器載入內存的順序。
例如:如果有兩個Servlet元素都含有<load-on-startup>子元素,則<load-on-startup>子元素值較小的Servlet將先被加載。如果<load-on-startup>子元素值為空或負值,則由Web容器決定什么時候加載Servlet。如果兩個Servlet的<load-on-startup>子元素值相同,則由Web容器決定先加載哪一個Servlet。<load-on-startup>1</load-on-startup>表示啟動容器時,初始化Servlet。
里面有很多部分用到了web.xml,關於他的步驟,這篇博客里面有寫【http://blog.csdn.net/believejava/article/details/43229361】,我自己也有總結,感興趣可以去我的博客里面看看。