Servelt学习笔记之二——使用Servlet提取表单中的数据
1、Servlet表單數據
在很多的情況下,我們需要在瀏覽器,Web服務器和后臺程序之間傳遞數據。瀏覽器使用兩種方法可將這些信息傳遞到Web服務器,分別為Get方法和Post方法。
1.1、Get方法:
Get方法向頁面請求發送已編碼的用戶信息。頁面和已編碼的信息中間用?字符分隔,如下所示:
GET方法是默認的從瀏覽器向Web服務器傳遞信息的方法,它會產生一個很長的字符串,出現在瀏覽器的地址欄中。如果您要向服務器傳遞的是密碼或其他的敏感信息,請不要使用GET方法.GET方法有大小限制:請求字符串中最多只能有1024個字符。
這些信息使用QUERY_STRING頭傳遞,并可以通過QUERY_STRING環境變量訪問,Servlet中使用doGet()方法處理這種類型的請求
1.2、Post方法:
另一個向后臺程序傳遞信息的比較可靠的方法是POST方法。POST方法打包信息的方式與GET方法基本相同,但是POST 方法不是把信息作為URL中?字符后的文本字符串進行發送,而是把這些信息作為一個單獨的消息。消息以標準輸出的形式傳到后臺程序,您可以解析和使用這些標準輸出。Servlet使用doPost()方法處理這種類型的請求。
Servlet 讀取表單數據
Servlet 處理表單數據,這些數據會根據不同的情況使用不同的方法自動解析:
- getParameter():您可以調用 request.getParameter() 方法來獲取表單參數的值。
- getParameterValues():如果參數出現一次以上,則調用該方法,并返回多個值,例如復選框。
- getParameterNames():如果您想要得到當前請求中的所有參數的完整列表,則調用該方法。
2、方法實例
2.1、獲取普通表單數據
2.1.1使用Get方法獲取數據
2.1.1.1使用URL的GET方法實例
我們使用 GET 方法向BServlet程序傳遞兩個值。
http://localhost:8080/MyServlet/BServlet?username=martin0319&password=admin
下面是處理 Web 瀏覽器輸入的BServlet.java Servlet 程序。我們將使用getParameter()方法,可以很容易地訪問傳遞的信息:
2.1.1.2使用Get方法獲取表單數據
在上面原有的基礎之上,在WEB-ROOT的目錄下新建一個FormData.html文件,文件的Action指向BServlet,method設定為Get
使用瀏覽器打開該HTML文件,username填寫administrator,password填寫admin,然后提交,自動跳轉到BServlet頁面,頁面顯示效果如下:
2.1.1、使用Post方法獲取數據:
依舊使用上述的BServlet項目,但是要做一些修改,在程序下添加doPost()方法,修改WEB-ROOT目錄下FormData.html文件,文件的Action指向BServlet,method設定為POST。效果如下:
FormData.html文件:
BServlet.java文件
使用瀏覽器打開該HTML文件,username填寫martin0319,password填寫martin0319,然后提交,自動跳轉到BServlet頁面,頁面顯示效果如下:
2.2、獲取復選框表單數據
2.2.1、使用Get方法獲取數據
在WEB-ROOT下創建CheckBoxData.html,文件Action指向CServlet,并且method定義為get;
<!DOCTYPE html> <html><head><title>FormData.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--></head><body><form action="CServlet" method="get"><input type="checkbox" name="swimming"/> Swimming<input type="checkbox" name="running"/> Running<input type="checkbox" name="tennis"/> Table Tennis<input type="submit" value="Choose Sport" /></form></body> </html>CServlet代碼如下:
package com.servlet.basic;//導入必須的包 import java.io.*; import javax.servlet.*; import javax.servlet.http.*;public class CServlet extends HttpServlet {// 處理 GET 方法請求的方法public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 設置響應內容類型response.setContentType("text/html");PrintWriter out = response.getWriter();String title = "Read The CheckBoxData";String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "+ "transitional//en\">";out.println(docType + "<html>" + "<head>" +"<title>" + title + "</title>"+"</head>" + "<body>"+"<h1 align=\"center\">" + title + "</h1>" + "<ul>"+"<li><b>Swimming:</b>:" + request.getParameter("swimming") + ""+"<li><b>Running:</b>:" + request.getParameter("running")+ "" +"<li><b>Table Tennis:</b>:"+ request.getParameter("tennis") + "" + "</ul>"+"</body>" +"</html>");}}運行效果如下:
2.2.2、使用Post方法獲取數據
修改在WEB-ROOT下CheckBoxData.html,文件Action指向CServlet,并且method定義為post;修改CServlet文件:
CheckBoxData.html修改如下:
<!DOCTYPE html> <html><head><title>FormData.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--></head><body><form action="CServlet" method="post"><input type="checkbox" name="swimming"/> Swimming<input type="checkbox" name="running"/> Running<input type="checkbox" name="tennis"/> Table Tennis<input type="submit" value="Choose Sport" /></form></body> </html>CServlet.java修改如下: package com.servlet.basic;//導入必須的包 import java.io.*; import javax.servlet.*; import javax.servlet.http.*;public class CServlet extends HttpServlet {// 處理 GET 方法請求的方法public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 設置響應內容類型response.setContentType("text/html");PrintWriter out = response.getWriter();String title = "Read The CheckBoxData";String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "+ "transitional//en\">";out.println(docType + "<html>" + "<head>" +"<title>" + title + "</title>"+"</head>" + "<body>"+"<h1 align=\"center\">" + title + "</h1>" + "<ul>"+"<li><b>Swimming:</b>:" + request.getParameter("swimming") + ""+"<li><b>Running:</b>:" + request.getParameter("running")+ "" +"<li><b>Table Tennis:</b>:"+ request.getParameter("tennis") + "" + "</ul>"+"</body>" +"</html>");}//處理 POST 方法請求的方法public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
運行效果如下:
轉載于:https://www.cnblogs.com/depp/p/5038706.html
總結
以上是生活随笔為你收集整理的Servelt学习笔记之二——使用Servlet提取表单中的数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java学习-----单例模式
- 下一篇: 还是畅通工程(1233 并查集+krus