java web-----servlet
Servlet(Server Applet),全稱JavaServlet,未有中文譯文。是用Java編寫的服務器端程序。其主要功能在於交互式地瀏覽和修改數據,生成動態Web內容。狹義的Servlet是指Java語言實現的一個接口,廣義的Servlet是指任何實現了這個Servlet接口的類,一般情況下,人們將Servlet理解為後者。
Servlet運行於支持Java的應用服務器中。從原理上講,Servlet可以響應任何類型的請求,但絕大多數情況下Servlet只用來擴展基於HTTP協議的Web服務器。[百度百科]
** 如何使用servlet?
#1.編寫一個繼承與HttpServlet的java類
#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; publicclass 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) throwsServletException, 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); } }
java web-----servlet