Request获取参数请求通用方式

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

 

目录

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

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

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

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


regist02.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获取参数请求通用方式_第1张图片

Request获取参数请求通用方式_第2张图片

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复选框测试:




    
    注册页面




游戏 学习

Request获取参数请求通用方式_第3张图片

Request获取参数请求通用方式_第4张图片

 

3.Enumeration 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 getParameterNmaes():获取所有请求得参数名称
        Enumeration 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获取参数请求通用方式_第5张图片

 

4.Map 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 getParameterMap():获取所有参数得map集合
        Map parameterMap = request.getParameterMap();

        //遍历
        Set 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获取参数请求通用方式_第6张图片

你可能感兴趣的:(网络方面学习)