實習培訓——Servlet(5)html
Java Servlet 是運行在 Web 服務器或應用服務器上的程序,它是做爲來自 Web 瀏覽器或其餘 HTTP 客戶端的請求和 HTTP 服務器上的數據庫或應用程序之間的中間層。java
使用 Servlet,您能夠收集來自網頁表單的用戶輸入,呈現來自數據庫或者其餘源的記錄,還能夠動態建立網頁。web
Servlet 執行如下主要任務:數據庫
Servlet 生命週期可被定義爲從建立直到毀滅的整個過程。如下是 Servlet 遵循的過程:瀏覽器
如今讓咱們詳細討論生命週期的方法。緩存
init 方法被設計成只調用一次。它在第一次建立 Servlet 時被調用,在後續每次用戶請求時再也不調用。所以,它是用於一次性初始化,就像 Applet 的 init 方法同樣。服務器
Servlet 建立於用戶第一次調用對應於該 Servlet 的 URL 時,可是您也能夠指定 Servlet 在服務器第一次啓動時被加載。cookie
當用戶調用一個 Servlet 時,就會建立一個 Servlet 實例,每個用戶請求都會產生一個新的線程,適當的時候移交給 doGet 或 doPost 方法。init() 方法簡單地建立或加載一些數據,這些數據將被用於 Servlet 的整個生命週期。app
init 方法的定義以下:post
public void init() throws ServletException { // 初始化代碼... }
service() 方法是執行實際任務的主要方法。Servlet 容器(即 Web 服務器)調用 service() 方法來處理來自客戶端(瀏覽器)的請求,並把格式化的響應寫回給客戶端。
每次服務器接收到一個 Servlet 請求時,服務器會產生一個新的線程並調用服務。service() 方法檢查 HTTP 請求類型(GET、POST、PUT、DELETE 等),並在適當的時候調用 doGet、doPost、doPut,doDelete 等方法。
下面是該方法的特徵:
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{ }
service() 方法由容器調用,service 方法在適當的時候調用 doGet、doPost、doPut、doDelete 等方法。因此,您不用對 service() 方法作任何動做,您只須要根據來自客戶端的請求類型來重寫 doGet() 或 doPost() 便可。
doGet() 和 doPost() 方法是每次服務請求中最經常使用的方法。下面是這兩種方法的特徵。
GET 請求來自於一個 URL 的正常請求,或者來自於一個未指定 METHOD 的 HTML 表單,它由 doGet() 方法處理。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet 代碼 }
POST 請求來自於一個特別指定了 METHOD 爲 POST 的 HTML 表單,它由 doPost() 方法處理。
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet 代碼 }
destroy() 方法只會被調用一次,在 Servlet 生命週期結束時被調用。destroy() 方法可讓您的 Servlet 關閉數據庫鏈接、中止後臺線程、把 Cookie 列表或點擊計數器寫入到磁盤,並執行其餘相似的清理活動。
在調用 destroy() 方法以後,servlet 對象被標記爲垃圾回收。destroy 方法定義以下所示:
public void destroy() { // 終止化代碼... }
Servlet 是服務 HTTP 請求並實現 javax.servlet.Servlet 接口的 Java 類。Web 應用程序開發人員一般編寫 Servlet 來擴展 javax.servlet.http.HttpServlet,並實現 Servlet 接口的抽象類專門用來處理 HTTP 請求。
package yuan; // 導入必需的 java 庫 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // 擴展 HttpServlet 類 public class HelloWorld extends HttpServlet { private String message; public void init() throws ServletException { // 執行必需的初始化 message = "Hello World"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置響應內容類型 response.setContentType("text/html"); // 實際的邏輯是在這裏 PrintWriter out = response.getWriter(); out.println("<h1>" + message + "</h1>"); } public void destroy() { // 什麼也不作 } }
固然,不能忘記在web.xml中配置:
<web-app> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping> </web-app>
Tomcat的端口我改成了8082,Servlet是個人項目名稱。
下面是一個簡單的 URL,將使用 GET 方法向 HelloForm 程序傳遞兩個值。
下面是處理 Web 瀏覽器輸入的 HelloForm.java Servlet 程序。咱們將使用 getParameter() 方法,能夠很容易地訪問傳遞的信息:
package yuan; import java.io.IOException; import java.io.PrintWriter; 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 HelloForm */ /*@WebServlet("/HelloForm")*/ public class HelloForm extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloForm() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置響應內容類型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String title = "使用 GET 方法讀取表單數據"; // 處理中文 String name = new String(request.getParameter("name").getBytes("ISO8859-1"), "UTF-8"); String docType = "<!DOCTYPE html> \n"; out.println( docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" + " <li><b>站點名</b>:" + name + "\n" + " <li><b>網址</b>:" + request.getParameter("url") + "\n" + "</ul>\n" + "</body></html>"); } // 處理 POST 方法請求的方法 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
記得配置web.xml
<web-app> <servlet> <servlet-name>HelloForm</servlet-name> <servlet-class>yuan.HelloForm</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloForm</servlet-name> <url-pattern>/HelloForm</url-pattern> </servlet-mapping> </web-app>
下面是一個簡單的實例,使用 HTML 表單和提交按鈕傳遞兩個值。咱們將使用相同的 Servlet HelloForm 來處理輸入。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>百度(www.baidu.com)</title> </head> <body> <form action="HelloForm" method="GET"> 網址名:<input type="text" name="name"> <br /> 網   址:<input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html>
而後能夠輸入 http://localhost:8082/Servlet/helloWorld.html
在這裏,使用帶有 POST 方法的 helloformpost.html 進行測試,以下所示:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鳥教程(runoob.com)</title> </head> <body> <form action="HelloForm" method="POST"> 網址名:<input type="text" name="name"> <br /> 網   址:<input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html>
查看結果
3.5 比較get和post方法的不一樣
get方法url是顯示的參數的
post方法,在地址欄並無顯示參數
菜鳥教程