Java Servlet學習筆記(一)

來源:互聯網
上載者:User

標籤:java   servlet   web.xml   web   

Servlet簡介

Servlet是運行在web容器上的小程式。這種程式使用Java程式設計語言實現。在通訊量大的伺服器上,Servlet的優點在於它們的執行速度快於CGI程式,各個使用者請求被啟用成單個程式中的一個線程,而無需建立單獨的進行,這意味著伺服器端處理請求的系統開銷明顯降低。


Servlet生命週期

伺服器收到用戶端的請求後:

1.web容器判斷相應的Servlet類是否已載入到記憶體,並建立了該Servlet的執行個體。如果是,則轉到第4步,否則第2步;

2.載入並建立該Servlet類的一個執行個體對象;

3.調用Servlet執行個體對象的init()方法;

4.建立一個用於封裝HTTP請求訊息的HttpServletRequest對象和一個代表HTTP響應的HttpServletResponse對象,然後調用servlet的service()方法並將這兩個對象做為參數傳遞;

5.web容器被停止或是重啟之前,調用Servlet的Destroy()方法消毀執行個體對象。


所有的servlet需要繼承於HttpServlet類,需要重寫其doGet和doPost方法。

package servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class Cart */@WebServlet("/Cart")public class Cart extends HttpServlet {private static final long serialVersionUID = 1L;private String action;/** * @see HttpServlet#HttpServlet() */public Cart() {super();// TODO Auto-generated constructor stub}/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse *      response) */protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse *      response) */protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {if (request.getParameter("action") != null) {this.action = request.getParameter("action");if (this.action.equals("add")) {this.addToCart(request, response);}else if (this.action.equals("remove")) {this.removeFromCart(request, response);}else if (this.action.equals("list")) {    this.showCart(request, response);}}}protected void addToCart(HttpServletRequest request,HttpServletResponse response) {Integer itemId = Integer.parseInt(request.getParameter("itemId"));}protected void showCart(HttpServletRequest request,HttpServletResponse response) {}protected void removeFromCart(HttpServletRequest request,HttpServletResponse response) {}}

寫好一個Servlet類之後,需要在位於WEB-INF目錄下的web.xml檔案中將其註冊。

註冊一個servlet需要以下兩做代碼,第一個是要將servlet類註冊到系統中,第二個則是將servlet的方問路徑註冊到系統中,這裡需要注意的是在url-pattern中一定要是以"/"開始,否則會提示404錯誤。

<servlet>    <servlet-name>SERVLET NAME</servlet-name>    <servlet-class>SERVLET CLASS NAME INCLUDED PACKAGE NAME</servlet-class><servlet><servlet-mapping>    <servlet-name>SERVLET NAME</servlet-name>    <url-pattern>/ACCESS PATH</url-pattern></servlet-mapping>

PS:WEB-INF目錄中存入的是訪問受限的檔案,如果將預設的index.jsp檔案或是其他無需存取控制的jsp檔案也放入這個目錄,可能會導致該瀏覽器無法讀取該檔案內容。



Java 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.