JavaWeb基礎-Request物件接收表單請求引數

.二丫.發表於2020-12-08

Request物件接收表單請求引數

程式碼實現:
demo01.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>request接受表單引數</h1>
    <form action="/JavaWebDemo_war_exploded/Servletdemo02" method="get">
        使用者名稱:<input type="text" name="username"><br>
        密碼:<input type="password" name="password"><br>
        性別:<input type="radio" name="sex" value="man"><input type="radio" name="sex" value="woman"><br>
        籍貫:<select name="city">
        <option value="beijing">北京市</option>
        <option value="xian">西安市</option>
        <option value="chengdu">成都市</option>
        <option value="shanghai">上海市</option>
        <option value="shenzhen">深圳市</option>
    </select><br>
        愛好:<input type="checkbox" name="hobby" value="basketball">籃球
        <input type="checkbox" name="hobby" value="foottball">足球
        <input type="checkbox" name="hobby" value="volleyball">排球
        <br>
        自我介紹:<textarea name="info" rows="3" cols="8"></textarea>
        <br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

效果:
在這裡插入圖片描述
ServletDemo02.java

package ServletDemo;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;


/*
Request接收表單引數
 */
public class ServletDemo02 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如果是GET請求就會執行doGet中的程式碼
        //接收使用者名稱和密碼
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("使用者名稱:" + username + "密碼:" + password);//使用者名稱:aaa密碼:123
        //接收性別和籍貫
        String sex = req.getParameter("sex");
        String city = req.getParameter("city");
        System.out.println("性別:" + sex);//性別:man
        System.out.println("籍貫:" + city);//籍貫:xian
        //接收愛好
        String[] hobbies = req.getParameterValues("hobby");//多個值的時候使用getParameterValue()來接受資料
        System.out.println("愛好:" + Arrays.toString(hobbies));//愛好:[basketball, foottball]
        //接收自我介紹
        String info = req.getParameter("info");
        System.out.println("自我介紹:" + info);//自我介紹:I have a pan ! I am a boy! I love you girl!

        //使用getParameterMap接收資料
        Map<String, String[]> map = req.getParameterMap();
        for (String key:map.keySet()) {
            String[] value = map.get(key);
            System.out.println(key+"  "+Arrays.toString(value));
        }
        /*
        username  [aaa]
        password  [123456]
        sex  [man]
        city  [xian]
        hobby  [basketball, foottball]
        info  [I have a pan ! I am a boy! I love you girl!]
         */
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如果是Post請求就會執行doPost中的程式碼
        doGet(req, resp);
    }
}

相關文章