request获取请求参数通用方式
阅读原文时间:2023年07月08日阅读:2

package com.hopetesting.web.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;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* @author newcityman
* @date 2019/8/31 - 18:20
*/
@WebServlet("/requestDemo06")
public class RequestDemo06 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("========");
System.out.println(username + ":::" + password);
String[] hobbies = request.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
System.out.println("===================================");
Enumeration parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String s = parameterNames.nextElement();
System.out.println(s);
String parameter = request.getParameter(s);
System.out.println(parameter);
}
System.out.println("++++++++++++++++++++++++++++++++++++");

    Map<String, String\[\]> parameterMap = request.getParameterMap();  
    Set<String> strings = parameterMap.keySet();  
    for (String name : strings) {  
        String\[\] values = request.getParameterValues(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 {  
    this.doPost(request, response);  
}  

}