Request(请求) :
Request:获取请求数据
Response:设置响应数据
Request继承体系:
使用request对象,查阅JavaEE API文档的HttpServeltRequest接口
Tomcat需要解析请求数据,封装为request对象,并且创建requests对象传递到service方法中,验证如下:
创建Servlet并输出HttpServletRequest的包名:
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("/demo4") public class Myservlet4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(req); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
输出结果如下:
Request获取请求数据:
请求行:
GET/request-demo/req1?username=zhangsan HTTP/1.1
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("/demo4") public class Myservlet4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //请求方式的获取方法:String getMethod() String method=req.getMethod(); System.out.println(method); //虚拟目录 (项目访问路径)的获取方法:StringgetContextPath() String contextPath=req.getContextPath(); System.out.println(contextPath); //URL(统一资源标识符)的获取方法:String getRequestURL() StringBuffer url=req.getRequestURL(); System.out.println(url); //访问路径的完整目录 String uri=req.getRequestURI(); System.out.println(uri); //请求参数的获取方法:String getQueryString() String queryString=req.getQueryString(); System.out.println(queryString); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
点击运行后在网页中输入请求参数以及访问路径:
输出如下:
请求头:
User-Agent:Mozilla/5.0 Chrome/91.0.4472.106
String getHeader(String name):根据请求头名称,获取值
举例:
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("/demo4") public class Myservlet4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取请求头:user-agent:浏览器的版本信息 String agent=req.getHeader("user-agent"); System.out.println(agent); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
获取的信息如下:
请求体:
username=superbaby&password=123
ServletInputStream getInputStream():获取字节输入流 BufferedReader getReader():获取字符输入流
举例:
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; @WebServlet("/demo4") public class Myservlet4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取post 请求体:请求参数 //1.获取字符输入流 BufferedReader br=req.getReader(); //读取数据 String line=br.readLine(); System.out.println(line); } }
a.html文件:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <form action="/mymavenweb4/demo4" method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit"> </form> </body> </html>
在网页中,输入下述内容:
点击回车,我们发现此时的路径变为了demo4,表示将上述数据提交到了路径为demo4的文件中
输出内容如下所示:
通过上述的方法我们知道GET获取请求参数是通过getQueryString()方法,而POST方式获取请求参数是通过getReader()方法,那么是否存在一种统一获取请求参数的方式,从而统一doGet和doPOST方法内的代码呢?
如下所示:
当我们获取到请求方式后,首先对其进行判断,如果是GET,则使用GET方式获取请求参数,反之使用POST
获取到值之后,request会将字符串进行解析。如下所示:
得到上述这些键值对后,request会将其存放在一个集合中,很多情况下,一个键值对中的值不止有一个,针对这种情况,request对于键值部分是采用字符数组的形式
Map<String,String[]> getParameterMap():获取所有参数Map集合
举例:
创建servlet类:
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; @WebServlet("/demo4") public class Myservlet4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //get请求逻辑 System.out.println("get......."); //获取所有参数map的集合 Map<String,String[]> map=req.getParameterMap(); for(String key:map.keySet()){ //username:zhangsan lisi System.out.println(key+":"); //获取值 String[] values=map.get(key); for(String value:values){ System.out.println(value+" "); } System.out.println(); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
创建web文件:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <form action="/mymavenweb4/demo4" method="get"> <input type="text" name="username"> <input type="password" name="password"> <input type="checkbox" name="hobby" value="1">游泳 <input type="checkbox" name="hobby" value="2">爬山 <input type="submit"> </form> </body> </html>
运行后在网页中输入以下内容:
点击回车后,出现以下内容:
控制台输出:
String[] getParameterValues(String name):根据名称获取参数值(数组)
在上述创建的servlet类的doGET方法中加入如下代码:
//根据key获取参数值,数组 String[] hobbies=req.getParameterValues("hobby"); for(String hobby:hobbies) { System.out.println(hobby); }
运行,在网页中输入如下内容:
回车后我们发现,未填写值的部分获取到的为空字符串
控制台输出:
String getParamter(String name):根据名称获取参数值(单个值)
在上述创建的servlet类的doGET方法中加入如下代码:
//根据key获取单个参数值 String username=req.getParameter("username"); String password=req.getParameter("password"); System.out.println("获取到的单个参数值为:"); System.out.println(username); System.out.println(password);
运行,在网页中输入如下内容:
回车:
控制台输出:
那么上述的这些方法是否也可用用于POST请求方式呢?
验证如下:
1:将刚在doget方法中书写的获取值的核心部分代码复制至dopost方法中,代码如下所示
//post请求逻辑 System.out.println("post......."); //获取所有参数map的集合 Map<String,String[]> map=req.getParameterMap(); for(String key:map.keySet()){ //username:zhangsan lisi System.out.println(key+":"); //获取值 String[] values=map.get(key); for(String value:values){ System.out.println(value+" "); } } System.out.println(); //根据key获取参数值,数组 String[] hobbies=req.getParameterValues("hobby"); System.out.println("获取到的参数值为:"); for(String hobby:hobbies) { System.out.println(hobby); } //根据key获取单个参数值 String username=req.getParameter("username"); String password=req.getParameter("password"); System.out.println("获取到的单个参数值为:"); System.out.println(username); System.out.println(password);
第二步:将web文件中的method改为post