javaweb—servlet的簡單入門使用
Servlet(Server Applet),全稱JavaServlet,未有中文譯文。是用Java編寫的伺服器端程式。其主要功能在於互動式地瀏覽和修改資料,生成動態Web內容。狹義的Servlet是指Java語言實現的一個介面,廣義的Servlet是指任何實現了這個Servlet介面的類,一般情況下,人們將Servlet理解為後者。
Servlet運行於支援Java的應用伺服器中。從原理上講,Servlet可以響應任何型別的請求,但絕大多數情況下Servlet只用來擴充套件基於HTTP協議的Web伺服器。[百度百科]
** 如何使用servlet?
#1.編寫一個繼承與HttpServlet
#2.重寫doGet和doPost方法
#3.配置web.xml檔案,配置servlet節點
MyFirstServlet.java
package com.ibatis01.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyFirstServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("my frist servlet"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
Web.xml
…… <servlet> <servlet-name>MyFirstServlet</servlet-name> <servlet-class>com.ibatis01.servlet.MyFirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyFirstServlet</servlet-name> <url-pattern>/MyFirst</url-pattern> </servlet-mapping> …… |
執行結果:
** URL匹配
url-pattern 瀏覽器輸入 精確匹配 /first http://localhost:8080/day/first /itcast/demo1 http://localhost:8080/day/itcast/demo1 模糊匹配 /* http://localhost:8080/day/任意路徑 /itcast/* http://localhost:8080/day/itcast/任意路徑 *.字尾名 http://localhost:8080/day/任意路徑.do *.do *.action *.html(偽靜態):並不是一個真的靜態檔案 預設路徑[預設路徑,儘量少用] / http://localhost:8080/day |
注意:
#1.路徑要不就是/開始,要不就是*開始,而且兩種不能混合在一起用。
#2.當多個servlet被同時匹配,精確匹配優先
#3.預設路徑:當路徑匹配不到任何自定義的servlet的url路徑,則去tomcat下找DefaultServlet到物件的應用下查詢對應的靜態檔案,如果還找不到就返回404 not found[先找動態資源,再找靜態檔案]
** servlet的生命週期
構造方法:第一次訪問servlet時候呼叫
init方法:建立完servlet時候呼叫
service方法:每次請求的時候呼叫
destroy方法:當servlet被銷燬時候被呼叫
注意:
#1. service方法本來是用來分發呼叫doPost方法還是doGet方法,如果我們重寫了service方法就不會呼叫doGet或者doPost方法。
#2. Init 方法有有引數和無引數兩種方法,有引數的其實還是會呼叫無參的,所以servlet方法初始化程式碼寫在無引數的init方法即可
#3. Servlet物件在tomcat中是單例項多執行緒的[servlet肯定用了多執行緒]
package com.ibatis01.servlet; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; publicclass MyFirstServlet extends HttpServlet { /** * 構造方法 */ public MyFirstServlet() { System.out.println("1. 我被建立了......"); } /** * init方法 */ @Override public void init(ServletConfig config) throws ServletException { System.out.println("2. init方法被呼叫......"); } /** * service方法 */ @Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { System.out.println("3. service方法被呼叫......"); } @Override public void destroy() { System.out.println("4. 呼叫destroy方法......"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet 方法被呼叫......"); response.getWriter().append("my frist servlet"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doPost 方法被呼叫......"); doGet(request, response); } } |
** servlet的自動載入
在web.xml中加入load-on-startup即可[中間的數值越大優先順序越低]
<servlet> <servlet-name>MyFirstServlet</servlet-name> <servlet-class>com.ibatis01.servlet.MyFirstServlet</servlet-class> <!-- 讓servlet在伺服器啟動的時候就建立 --> <load-on-startup>1</load-on-startup> </servlet> |
** servlet的執行緒安全問題
由於servlet是單例項多執行緒的,所以有可能引發多執行緒問題!
package com.ibatis01.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ThreadServlet extends HttpServlet { private int num=0; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { num++; Thread.sleep(2000); response.getWriter().append(this.toString()+"<br/>").append("num="+num); } catch (InterruptedException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
結果是:[出現了想要的問題]
可以通過加鎖來來解決問題
package com.ibatis01.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ThreadServlet extends HttpServlet { private int num = 0; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { synchronized (this) { try { num++; Thread.sleep(2000); response.getWriter().append(this.toString() + "<br/>").append("num=" + num); } catch (InterruptedException e) { e.printStackTrace(); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |