亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解

 更新時間:2022年04月18日 15:55:06   作者:Distance-X  
本文主要介紹了Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

表單代碼

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
<form  action="getParamter" method="get">
    <input type="text" name="username" placeholder="請輸入用戶名"></br>
    <input type="password" name="password" placeholder="請輸入密碼"></br>
    <input type="checkbox" name="hobby" value="study">學習
    <input type="checkbox" name="hobby" value="basktball">打籃球
    <input type="checkbox" name="hobby" value="sleep">睡覺</br>
    <input type="submit" value="提交">
</form>
</body>
</html>

request.getParamter(String name);通過name獲取值

代碼片段

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通過name屬性獲取值
        String name = request.getParameter("username");
        System.out.println("獲取到的姓名是:"+name);
        String pwd  = request.getParameter("password");
        System.out.println("獲取到的密碼是: "+pwd);
    }

運行結果

在這里插入圖片描述

request.getParamterValues(String name);通過name獲取value值(一般用于復選框獲取值) 代碼片段

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通過name屬性獲取value值
        String[] names = request.getParameterValues("username");
        for (String name : names) {
            System.out.println("獲取得到的姓名是 " + name);
        }

        String[] hobbys = request.getParameterValues("hobby");
        for (String hobby : hobbys) {
            System.out.println("復選框的值是: " + hobby);
        }
}

運行結果

在這里插入圖片描述

總結:此方法雖然是通過name獲取value值,但是對于普通輸入框是直接獲取的是它們的輸入值,類似第一種方法,普通輸入框獲取值可以使用但是不建議使用,一般用來獲取復選框的值

request.getParameterNames();直接獲取表單所有對象的name值,返回值是枚舉集合

代碼片段

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通過name屬性獲取value值
        Enumeration<String> names2 = request.getParameterNames();
        while (names2.hasMoreElements()){
            String name =names2.nextElement();
            System.out.println(name);
        }
    }

運行結果

在這里插入圖片描述

總結:獲取到name值以后采用第一種方法獲取真實的值

request.getParameterMap();直接獲取表單所有對象的name值以及數(shù)據(jù)

該方法的返回值是map集合,集合key是String類型,value是Sting類型的數(shù)組

代碼片段

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通過map代表所有的值 ,集合中的key代表表單name值  value代表表單數(shù)據(jù)
        Map<String, String[]> map = request.getParameterMap();
        //通過keySet遍歷集合
        Set<String> keySet = map.keySet();
        for(String key : keySet){
            System.out.println("表單的name值: "+key);
            //通過key值獲取所有value值
            String[] values = map.get(key);
            for(String value : values ){
                System.out.println("表單中的數(shù)據(jù):  "+value);
            }
        }
    }

運行結果

在這里插入圖片描述

總結:該方法獲取所有值,所以有復選框的時候可以選擇這種方法

到此這篇關于Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解的文章就介紹到這了,更多相關Javaweb Request獲取表單數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論