1、Servlet的工作流程
- 客戶端(瀏覽器)提交對Servlet調用的Get或Post請求。
- 服務端接到請求後,如果對Servlet是第一次調用,則實例化這個Servlet。
- 服務器調用該Servlet對象的service()方法。
- 服務器產生動態的回覆內容,並將內容發送給service()方法。
2、Servlet表單數據
客戶端通過瀏覽器發送給Web服務器的請求一共有7種,即POST、GET、PUT、DELETE、OPTIONS、HEAD和TRACE。實際web編程中,用到比較多的分別爲 GET 方法和 POST 方法。
(1)GET 方法
GET 方法是默認的從瀏覽器向 Web 服務器傳遞信息的方法,它會產生一個很長的字符串,出現在瀏覽器的地址欄中。GET 方法有大小限制:請求字符串中最多只能有 1024 個字符。
Servlet 使用 doGet() 方法處理這種類型的請求。
(2)POST方法
POST 方法打包信息的方式與 GET 方法基本相同,但是 POST 方法不是把信息作爲 URL 中 ? 字符後的文本字符串進行發送,而是把這些信息作爲一個單獨的消息。消息以標準輸出的形式傳到後臺程序
Servlet 使用 doPost() 方法處理這種類型的請求。
“POST”請求是通過HTML中表單“Form”進行發送的,表單中寶礦力不同形式的輸入,如Input text、Input password、Input radio、Input checkbox等。
3、doGet、doPost和service方法
在servlet開發中,以doGet()和doPost()分別處理get和post方法。首先判斷請求時是get還是post,如果是get就調用doGet(), 如果是post就調用doPost()。
(1) Service是類GenericServlet中的方法,每次客戶向服務器發出請求時,服務器就會調用這個方法。程序員如果想對客戶的請求進行響應的話就必須覆蓋這個方法,並在這個方法中加入自己的代碼來實現對客戶的響應。
Servlet 類中有 service() 方法,則優先調用 Service 方法。然後纔會調用自己覆寫的doPost()或是doGet()方法。
(2)doGet()表示,當客戶端是使用get方式請求該servlet時,那麼就會觸發執行doGet()方法中的代碼。
(3)doPost()表示,當客戶端是使用post方式請求該servlet時,那麼就會觸發執行doPost()方法中的代碼。
參考:https://blog.csdn.net/qq_40301026/article/details/90076936
4、HTML表單基礎知識
Form標記基本語法:
<form method="post | get" action="/servlet/addMessage">
<!--添加form標記-->
</form>
說明:method用於設置將表單的數據傳遞給Web服務端的方法,post或get;action指明處理這個表單的Servlet程序所在的URL地址,默認以form所在文件的URL爲設置值。
5、表單應用案例
建立賬號、密碼登錄頁面
(1)eclipse下新建一個Dynamic Web Project項目如下
建立三個文件:
- login.html 登錄界面
- web.xml 配置文件
- MyServlet.java 編寫servlet
(2)編寫login.html 登錄界面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="myServlet">
<table width="313" border="0">
<tr>
<td width="85">用戶名:</td>
<td width="218"><input name="username" type="text" id="username" size="18" maxlength="10"></td>
</tr>
<tr>
<td width="85">密碼:</td>
<td width="218"><input name="password" type="password" id="password" size="18" maxlength="10"></td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="提交"></td>
<td><input type="reset" name="reset" id="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
注意<form> 的action爲web.xml文件裏配置的servlet-name的名字,引用時前面不要加“/”,該位置設置不正確會出現404錯誤。
(3) MyServlet.java編寫
該類裏面重新doPost方法
package com.herry.bean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.connector.Response;
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//設置響應內容
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>表單測試</TITLE></HEAD>");
out.println("<BODY>");
req.setCharacterEncoding("utf-8");
out.println("input username:"+req.getParameter("username")+"<br>");
out.println("input password:"+req.getParameter("password")+"<br>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
(4)配置 web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<!-- 配置servlet -->
<!-- 配置servlel類路徑 -->
<servlet>
<servlet-name>myServlet</servlet-name> <!-- servlet名字自定義 -->
<servlet-class>com.herry.bean.MyServlet</servlet-class> <!-- servlet類的路徑-->
</servlet>
<!-- 配置servlel訪問方式 -->
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
(5)項目 中WebContent的所有文件部署到Tomcat下,開啓Tomcat;瀏覽器中輸入
action的配置還要其他方法見以下鏈接:
https://blog.csdn.net/zeephom/article/details/79609999