Request获取请求参数通用方式指的是不论get还是post请求方式都可以使用下列方式来获取请求参数,这样的方法一共有四种,下面来为大家一一介绍。
1.String getParameter(String name):根据参数名称获取参数值
为了验证这些方法是否可行,首先需要再web项目中先创建了html文件,html中写一些输入框,通过输入的内容是否被打印来判断这些方法是否正确。
regist2.html
注册页面
随后直接创建一个Servlet文件,将注解配置中的路径和action中的路径保持一致(便于之后网页的访问)
Requestdemo6.java
package cn.itcast.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.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet("/requested6")
public class Requestdemo6 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 获取请求参数
this.doPost(request,response);
}
}
输入框的账号和密码都输入123,打印结果:
post
123
可以看出username被打印
2.String[] getParameterValues(String name):根据参数名称获取参数值的数组
regist2.html
注册页面
Requestdemo6.java
package cn.itcast.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.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet("/requested6")
public class Requestdemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据参数名称获取参数值的数组
String[] hobbies = request.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get 获取请求参数
this.doPost(request,response);
}
}
打印结果:
game
study
此方法可以打印出该参数名称的所有参数值,而第一个方法只能打印参数名称的一个参数值
3.Enumeration getParameterNames():获取所有请求的参数名称
html文件不改变
Requestdemo6.java
package cn.itcast.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.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet("/requested6")
public class Requestdemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取所有请求的参数名称
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 {
//get 获取请求参数
this.doPost(request,response);
}
}
打印结果:
username
123
-------------
password
1234
-------------
hobby
game
-------------
此方法可以获取所有参数名称,这里我们根据参数名称再调用第一个方法打印出参数值。
4.Map
Requestdemo6.java
package cn.itcast.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.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet("/requested6")
public class Requestdemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取所有参数的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 {
//get 获取请求参数
this.doPost(request,response);
}
}
打印结果与3一样
总结:这四种方法是获取请求参数通用方法,其中第一,第四种方法较为常用。