Servelt学习笔记之二——使用Servlet提取表单中的数据
1、Servlet表單數(shù)據(jù)
在很多的情況下,我們需要在瀏覽器,Web服務(wù)器和后臺(tái)程序之間傳遞數(shù)據(jù)。瀏覽器使用兩種方法可將這些信息傳遞到Web服務(wù)器,分別為Get方法和Post方法。
1.1、Get方法:
Get方法向頁(yè)面請(qǐng)求發(fā)送已編碼的用戶信息。頁(yè)面和已編碼的信息中間用?字符分隔,如下所示:
GET方法是默認(rèn)的從瀏覽器向Web服務(wù)器傳遞信息的方法,它會(huì)產(chǎn)生一個(gè)很長(zhǎng)的字符串,出現(xiàn)在瀏覽器的地址欄中。如果您要向服務(wù)器傳遞的是密碼或其他的敏感信息,請(qǐng)不要使用GET方法.GET方法有大小限制:請(qǐng)求字符串中最多只能有1024個(gè)字符。
這些信息使用QUERY_STRING頭傳遞,并可以通過QUERY_STRING環(huán)境變量訪問,Servlet中使用doGet()方法處理這種類型的請(qǐng)求
1.2、Post方法:
另一個(gè)向后臺(tái)程序傳遞信息的比較可靠的方法是POST方法。POST方法打包信息的方式與GET方法基本相同,但是POST 方法不是把信息作為URL中?字符后的文本字符串進(jìn)行發(fā)送,而是把這些信息作為一個(gè)單獨(dú)的消息。消息以標(biāo)準(zhǔn)輸出的形式傳到后臺(tái)程序,您可以解析和使用這些標(biāo)準(zhǔn)輸出。Servlet使用doPost()方法處理這種類型的請(qǐng)求。
Servlet 讀取表單數(shù)據(jù)
Servlet 處理表單數(shù)據(jù),這些數(shù)據(jù)會(huì)根據(jù)不同的情況使用不同的方法自動(dòng)解析:
- getParameter():您可以調(diào)用 request.getParameter() 方法來獲取表單參數(shù)的值。
- getParameterValues():如果參數(shù)出現(xiàn)一次以上,則調(diào)用該方法,并返回多個(gè)值,例如復(fù)選框。
- getParameterNames():如果您想要得到當(dāng)前請(qǐng)求中的所有參數(shù)的完整列表,則調(diào)用該方法。
2、方法實(shí)例
2.1、獲取普通表單數(shù)據(jù)
2.1.1使用Get方法獲取數(shù)據(jù)
2.1.1.1使用URL的GET方法實(shí)例
我們使用 GET 方法向BServlet程序傳遞兩個(gè)值。
http://localhost:8080/MyServlet/BServlet?username=martin0319&password=admin
下面是處理 Web 瀏覽器輸入的BServlet.java Servlet 程序。我們將使用getParameter()方法,可以很容易地訪問傳遞的信息:
2.1.1.2使用Get方法獲取表單數(shù)據(jù)
在上面原有的基礎(chǔ)之上,在WEB-ROOT的目錄下新建一個(gè)FormData.html文件,文件的Action指向BServlet,method設(shè)定為Get
使用瀏覽器打開該HTML文件,username填寫administrator,password填寫admin,然后提交,自動(dòng)跳轉(zhuǎn)到BServlet頁(yè)面,頁(yè)面顯示效果如下:
2.1.1、使用Post方法獲取數(shù)據(jù):
依舊使用上述的BServlet項(xiàng)目,但是要做一些修改,在程序下添加doPost()方法,修改WEB-ROOT目錄下FormData.html文件,文件的Action指向BServlet,method設(shè)定為POST。效果如下:
FormData.html文件:
BServlet.java文件
使用瀏覽器打開該HTML文件,username填寫martin0319,password填寫martin0319,然后提交,自動(dòng)跳轉(zhuǎn)到BServlet頁(yè)面,頁(yè)面顯示效果如下:
2.2、獲取復(fù)選框表單數(shù)據(jù)
2.2.1、使用Get方法獲取數(shù)據(jù)
在WEB-ROOT下創(chuàng)建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;//導(dǎo)入必須的包 import java.io.*; import javax.servlet.*; import javax.servlet.http.*;public class CServlet extends HttpServlet {// 處理 GET 方法請(qǐng)求的方法public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 設(shè)置響應(yīng)內(nèi)容類型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>");}}運(yùn)行效果如下:
2.2.2、使用Post方法獲取數(shù)據(jù)
修改在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;//導(dǎo)入必須的包 import java.io.*; import javax.servlet.*; import javax.servlet.http.*;public class CServlet extends HttpServlet {// 處理 GET 方法請(qǐng)求的方法public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 設(shè)置響應(yīng)內(nèi)容類型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 方法請(qǐng)求的方法public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
運(yùn)行效果如下:
轉(zhuǎn)載于:https://www.cnblogs.com/depp/p/5038706.html
總結(jié)
以上是生活随笔為你收集整理的Servelt学习笔记之二——使用Servlet提取表单中的数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java学习-----单例模式
- 下一篇: 还是畅通工程(1233 并查集+krus