Request_获取请求参数通用方式介绍
1.其他功能:
1.获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数
1.String getParameter(String name):根据参数名称获取参数值 username=zs&password=123
2.String [] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game
3.Enumration<String> getParmeterNames():获取所有请求的参数名称
4.Map<String ,String []> getParameterMap():获取所有参数的map集合
2.请求转发:
3.共享数据:
4.获取ServletContext
Request_获取请求参数通用方式演示
1.String getParameter(String name):根据参数名称获取参数值 username=zs&password=123
2.String [] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game
3.Enumration<String> getParmeterNames():获取所有请求的参数名称
4.Map<String ,String []> getParameterMap():获取所有参数的map集合
RequestDemo6
package com.example.day14_servlethttprequest.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.Map; import java.util.Set; @WebServlet("/requestDemo6") public class RequestDemo6 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //post获取请求参数 //根据参数名称获取参数值 String username = request.getParameter("username"); /*System.out.println("get"); System.out.println(username);*/ //根据参数名称获取参数值的数组 String[] hobbies = request.getParameterValues("hobby"); /* for (String hobby:hobbies) { System.out.println(hobby); }*/ //获取所以请求的参数名称 Enumeration<String> names = request.getParameterNames(); /* while (names.hasMoreElements()){ String s = names.nextElement(); System.out.println(s); String parameter = request.getParameter(s); System.out.println(parameter); System.out.println("*********************"); }*/ //获取所有参数的map集合 Map<String, String[]> parameterMap = request.getParameterMap(); //遍历 Set<String> keyset = parameterMap.keySet(); for (String key :keyset){ //获取键获取值 String[] values = parameterMap.get(key); System.out.println(key); for (String value:values) { System.out.println(value); } System.out.println("*************************"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //get 获取请求参数 /* //根据参数名称获取参数值 String username = request.getParameter("username"); System.out.println("post"); System.out.println(username);*/ this.doPost(request,response); } }
regist2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册页面</title> </head> <body> <form action="requestDemo6" method="get"> <input type="text" placeholder="请输入账号" name="username"><br> <input type="text" placeholder="请输入密码" name="password"><br> <input type="checkbox" name="hobby" value="篮球">篮球 <input type="checkbox" name="hobby" value="rap">rap <br> <input type="submit"value="注册"> </form> </body> </html>
标签:请求,Request,System,获取,参数,println,out,String From: https://www.cnblogs.com/x3449/p/17076349.html