Servelt學習筆記之二——使用Servlet提取表單中的數據


1、Servlet表單數據

在很多的情況下,我們需要在瀏覽器,Web服務器和后台程序之間傳遞數據。瀏覽器使用兩種方法可將這些信息傳遞到Web服務器,分別為Get方法和Post方法。

1.1、Get方法:

Get方法向頁面請求發送已編碼的用戶信息。頁面和已編碼的信息中間用?字符分隔,如下所示:

http://www.test.com/hello?key1=value1&key2=value2

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

package com.servlet.basic; //導入必須的包
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; //拓展繼承HttpServlet類
public class BServlet extends HttpServlet { // 處理 GET 方法請求的方法
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置響應內容類型
        response.setContentType("text/html"); //利用創建PrintWriter對象,將內容輸出到頁面上
        PrintWriter out = response.getWriter(); String title = "Using GET Method to Read Form Data"; 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>username</b>:" + request.getParameter("username")+ 
                        "<li><b>password</b>:" + request.getParameter("password")+"</ul>" + 
                    "</body>" +
                "</html>"); } }

下面是處理 Web 瀏覽器輸入的BServlet.java Servlet 程序。我們將使用getParameter()方法,可以很容易地訪問傳遞的信息:

image

2.1.1.2使用Get方法獲取表單數據

在上面原有的基礎之上,在WEB-ROOT的目錄下新建一個FormData.html文件,文件的Action指向BServlet,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="BServlet" method="get">
 username:<input type="text" name="username"/><br>
 password:<input type="password" name="password"/><br>
            <input type="submit" value="submit"/>
        </form>
    </body>
</html>

使用瀏覽器打開該HTML文件,username填寫administrator,password填寫admin,然后提交,自動跳轉到BServlet頁面,頁面顯示效果如下:

2

3

2.1.1、使用Post方法獲取數據:

依舊使用上述的BServlet項目,但是要做一些修改,在程序下添加doPost()方法,修改WEB-ROOT目錄下FormData.html文件,文件的Action指向BServlet,method設定為POST。效果如下:

FormData.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="BServlet" method="post">
 username:<input type="text" name="username"/><br>
 password:<input type="password" name="password"/><br>
            <input type="submit" value="submit"/>
        </form>
    </body>
</html>

BServlet.java文件

package com.servlet.basic; //導入必須的包
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; //拓展繼承HttpServlet類
public class BServlet extends HttpServlet { // 處理doGet方法
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置響應內容類型
        response.setContentType("text/html"); //利用創建PrintWriter對象,將內容輸出到頁面上
        PrintWriter out = response.getWriter(); String title = "Using GET Method to Read Form Data"; 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>username</b>:" + request.getParameter("username")+ 
                        "<li><b>password</b>:" + request.getParameter("password")+"</ul>" + 
                    "</body>" +
                "</html>"); } //doPost方法
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

使用瀏覽器打開該HTML文件,username填寫martin0319,password填寫martin0319,然后提交,自動跳轉到BServlet頁面,頁面顯示效果如下

4

5

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>");
    }

}

運行效果如下:

3

2

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);
    }

}

運行效果如下:

4

5


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM