获取请求参数通用方式
获取请求参数通用方式
String getParameter(String name)
// 根据参数名称获取参数值
String[] getParameterValues(String name)
// 根据参数名称获取参数值的数组
Enumeration<String> getParameterNames()
// 获取所有请求的参数名称
Map<String,String[]> getParameterMap()
// 获取所有参数的map集合
无论get还是post请求方式都可以使用这些方法来获取请求参数
根据参数获取参数值
-
创建一个html文件:login02.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <body> <form action="/RequestAndResponse_war_exploded/study/request/demo04" method="post"> <input type="email" name="userEmail" placeholder="请输入您的邮箱"><br> <input type="password" name="userPassword" placeholder="请输入您的密码"><br> <input type="submit" value="post登录"> </form> <br> <form action="/RequestAndResponse_war_exploded/study/request/demo04" method="get"> <input type="email" name="userEmail" placeholder="请输入您的邮箱"><br> <input type="password" name="userPassword" placeholder="请输入您的密码"><br> <input type="submit" value="get登录"> </form> </body> </html>
-
创建一个类,RequestDemo04.java
package study.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("/study/request/demo04") public class RequestDemo04 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userEmail = req.getParameter("userEmail"); String userPassword = req.getParameter("userPassword"); System.out.println("userEmail = " + userEmail + " " + "userPassword = " + userPassword); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userEmail = req.getParameter("userEmail"); String userPassword = req.getParameter("userPassword"); System.out.println("userEmail = " + userEmail + " " + "userPassword = " + userPassword); } }
-
上面的 doGet() 方法 和 doPost() 方法中的方法体内容一样,可以进行修改:
public class RequestDemo04 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userEmail = req.getParameter("userEmail"); String userPassword = req.getParameter("userPassword"); System.out.println("userEmail = " + userEmail + " " + "userPassword = " + userPassword); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
-
浏览器访问:http://localhost:8080/RequestAndResponse_war_exploded/login02.html
-
Post 请求
控制台输出:
userEmail = post@icloud.com userPassword = 123456
页面跳转到:http://localhost:8080/RequestAndResponse_war_exploded/study/request/demo04
-
Get 请求
控制台输出:
userEmail = get@icloud.com userPassword = abcdef
-
根据参数名称获取参数值的数组
-
创建一个html文件:login03.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>爱好</title> </head> <body> <form action="/RequestAndResponse_war_exploded/study/request/demo05" method="post"> <input type="checkbox" name="hobby" value="study"> 学习 <input type="checkbox" name="hobby" value="game"> 游戏 <br> <input type="submit" value="post提交"> </form> <br> <form action="/RequestAndResponse_war_exploded/study/request/demo05" method="get"> <input type="checkbox" name="hobby" value="study"> 学习 <input type="checkbox" name="hobby" value="game"> 游戏 <br> <input type="submit" value="get提交"> </form> </body> </html>
-
创建一个demo.java:RequestDemo05.java
package study.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("/study/request/demo05") public class RequestDemo05 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 根据参数名称获取参数值的数组 String[] hobbies = req.getParameterValues("hobby"); // 参数名称为hobby for (String hobby : hobbies) { System.out.println(hobby); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
-
启动服务器,访问:http://localhost:8080/RequestAndResponse_war_exploded/login03.html
这里两种提交方式都一样!!!
-
选择:study,并提交,控制台输出
study
浏览器页面跳转到:http://localhost:8080/RequestAndResponse_war_exploded/study/request/demo05
-
选择:study、game并提交,控制台输出
study game
浏览器页面跳转到:http://localhost:8080/RequestAndResponse_war_exploded/study/request/demo05
-
获取所有请求的参数名称
-
创建一个类,RequestDemo06.java
package study.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("/study/request/demo06") public class RequestDemo06 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取所有请求参数名称 Enumeration<String> parameterNames = req.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = parameterNames.nextElement(); // 输出参数名称,已经根据参数名称输出对应的值 System.out.println(parameterName + ":" + req.getParameter(parameterName) + " "); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
-
创建余个html文件:login04.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户爱好</title> </head> <body> <form action="/RequestAndResponse_war_exploded/study/request/demo06" method="get"> <label><input type="email" name="userEmail" placeholder="请输入您的邮箱"></label><br> <label><input type="password" name="userPassword" placeholder="请输入您的密码"></label><br> <label><input type="checkbox" name="hobby1" value="study"></label> 学习 <label><input type="checkbox" name="hobby2" value="game"></label> 游戏 <br> <input type="submit" value="提交"> </form> </body> </html>
-
启动服务器,访问:http://localhost:8080/RequestAndResponse_war_exploded/login04.html
提交,控制台输出:
userEmail:stringbug@icloud.com userPassword:123456 hobby1:study hobby2:game
获取所有参数的map集合
-
创建一个html文件:login05.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户爱好</title> </head> <body> <form action="/RequestAndResponse_war_exploded/study/request/demo07" method="get"> <label><input type="email" name="userEmail" placeholder="请输入您的邮箱"></label><br> <label><input type="password" name="userPassword" placeholder="请输入您的密码"></label><br> <label><input type="checkbox" name="hobby" value="study"></label> 学习 <label><input type="checkbox" name="hobby" value="game"></label> 游戏 <br> <input type="submit" value="提交"> </form> </body> </html>
login05.html 与 login04.html 的差别是:将hobby1、hobby2均改为了hobby
将路径中的demo06改为了demo07
-
创建一个类:RequestDemo07.java
package study.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("/study/request/demo07") public class RequestDemo07 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取所有请求参数的map集合 Map<String, String[]> parameterMap = req.getParameterMap(); // 获取所有的请求名称 Set<String> parameterNames = parameterMap.keySet(); for (String parameterName : parameterNames) { // 输出每个请求名称 System.out.println("请求名称 -- " + parameterName); // 根据请求名称获取对应的请求值 String[] parameterValues = req.getParameterValues(parameterName); for (String parameterValue : parameterValues) { // 输出每个请求值 System.out.println(parameterName + ":" + parameterValue); } } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
-
启动服务器,访问:http://localhost:8080/RequestAndResponse_war_exploded/login05.html
-
提交,控制台输出:
请求名称 -- userEmail userEmail:stringbug@icloud.com 请求名称 -- userPassword userPassword:123456 请求名称 -- hobby hobby:study hobby:game
参考文献
无