java web-----servlet

來源:互聯網
上載者:User

標籤:基於   通過   初始化   lin   配置   src   模糊比對   代碼   pre   

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;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);         }}

 

java web-----servlet

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.