欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Request获取参数请求通用方式

程序员文章站 2022-07-14 22:33:35
...

1.获取请求参数通用方式

 

目录

1.String getParameter(String name):根据参数名称获取参数值

2.String[] getParameterValues(String name):根据参数名称获取参数值得数组

3.Enumeration getParameterNmaes():获取所有请求得参数名称

4.Map getParameterMap():获取所有参数得map集合,string[]>


regist02.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
<form action="/RequestDemo06" method="post">
    <input type="text" placeholder="请输入用户名" name="username"><br>
    <input type="password" placeholder="请输入密码"  name="password" ><br>
    <input type="submit" value="注册">
</form>
</body>
</html>

 

1.String getParameter(String name):根据参数名称获取参数值

package servlet.http.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( "/RequestDemo06")
public class RequestDemo06 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //post获取请求参数
        //根据参数名称获取参数值
        String username = request.getParameter("username");
        System.out.println("post");
        System.out.println(username);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //get获取请求参数
        //根据参数名称获取参数值
        /*String username = request.getParameter("username");
        System.out.println("get");
        System.out.println(username);*/

        this.doPost(request,response);
    }
}

Request获取参数请求通用方式

Request获取参数请求通用方式

2.String[] getParameterValues(String name):根据参数名称获取参数值得数组

package servlet.http.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( "/RequestDemo06_02")
public class RequestDemo06_02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        // 2.String[] getParameterValues(String name):根据参数名称获取参数值得数组
        String[] hobbies = request.getParameterValues("hobby");
        for (String hobby:hobbies) { //foreach
            System.out.println(hobby);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
}

regist03.html复选框测试:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
<form action="/RequestDemo06_02" method="post">
    <input type="text" placeholder="请输入用户名" name="username"><br>
    <input type="password" placeholder="请输入密码"  name="password" ><br>

    <input type="checkbox" name="hobby" value="game">游戏
    <input type="checkbox" name="hobby" value="study">学习
    <br>

    <input type="submit" value="注册">
</form>
</body>
</html>

Request获取参数请求通用方式

Request获取参数请求通用方式

 

3.Enumeration<String> getParameterNmaes():获取所有请求得参数名称

package servlet.http.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet( "/RequestDemo06_03")
public class RequestDemo06_03 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        // 3.Enumeration<String> getParameterNmaes():获取所有请求得参数名称
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String name = parameterNames.nextElement();
            System.out.println(name);

            String value = request.getParameter(name); //不是数组 对于多选框只能接收一个值
            System.out.println(value);
            System.out.println("-----------------");


        }
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
}

 

Request获取参数请求通用方式

 

4.Map<String,String[]> getParameterMap():获取所有参数得map集合

package servlet.http.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Set;


@WebServlet( "/RequestDemo06_04")
public class RequestDemo06_04 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //    4.Map<String,String[]> getParameterMap():获取所有参数得map集合
        Map<String, String[]> parameterMap = request.getParameterMap();

        //遍历
        Set<String> keyset = parameterMap.keySet();

        for (String name:keyset) { //迭代

            //根据键获取值
            String[] values = parameterMap.get(name);
            System.out.println(name);

            for (String value:values){
                System.out.println(value);
            }

            System.out.println("-----------------------");
        }



    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {




    }
}

Request获取参数请求通用方式