一、Request继承体系
1、Tomcat需要解析请求数据,封装为request对象,并且创建request对象传递到service方法中
2、使用request对象,查阅JavaEE API文档的HttServletRequest接口
代码示例查看request全类名:
import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet("/demo2") public class ServletDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
访问demo2路径:
控制台输出全类名:
二、Request获取请求数据
请求数据分为3部分:
1、请求行:
GET/request-demo/req1?username=zhangsan HTTP/1.1
String getMethod():获取请求方式:GET
String getContextPath():获取虚拟目录(项目访问路径):/request-demo
StringBuffer getRequestURL:获取URL(统一资源定位符):http://localhost:8080/request-demo/req1
String getRequestURL():获取URL(统一资源标识符):/request-demo/req1
String getQueryString():获取请求参数(GET方式):username=zhangsan&password=123
以上方法代码示例:
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("/req1") public class RequestDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //String getMethod() :获取请求方式:GET String method = req.getMethod(); System.out.println(method);//GET //String getContextPath():获取虚拟目录(项目访问路径):/request-demo String contextPath = req.getContextPath(); System.out.println(contextPath); //StringBuffer getRequestURL:获取URL(统一资源定位符):http://localhost:8080/request-demo StringBuffer url = req.getRequestURL(); System.out.println(url.toString()); //String getRequestURI():获取URL(统一资源标识符):/request-demo/req1 String uri = req.getRequestURI(); System.out.println(uri); //String getQueryString():获取请求参数(GEI方式):username=zhangsan String queryString = req.getQueryString(); System.out.println(queryString); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
访问req1路径:
控制台输出:
2、请求头:
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.BufferedReader; import java.io.IOException; @WebServlet("/req1") public class RequestDemo1 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 { } }
访问req1路径:
控制台输出:
3、请求体:
username=superbaby&password=123
ServletInputStream getInputStream():获取字节输入流
BufferedReader getReader():获取字符输入流
post请求需要表单,req.html代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/Tomcate-demo2/req4" method="get"> <input type="text" name="username"><br> <input type="password" name="password"><br> <input type="submit"> </form> </body> </html>
获取post请求参数代码:
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("/req1") public class RequestDemo1 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(); //2、读取数量 String line = br.readLine(); System.out.println(line); } }
访问req.html,并输出参数提交:
控制台输出: