一、request和response的原理
request和response对象是由Tomcat服务器创建的
- request对象是来获取请求数据的
- response对象是设置响应数据的
request执行原理
二、request获取请求行
1、获取请求消息数据
获取请求行数据,可查看ServletRequest文档
代码如下:
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("/test01")
public class test01_Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("hahahahhh");
String method = request.getMethod();//掌握
System.out.println("获取到的请求方法:"+method);
String contextPath = request.getContextPath();//掌握
System.out.println("获取到的虚拟目录:"+contextPath);
String servletPath = request.getServletPath();//掌握
System.out.println("获取到的servlet路径:"+servletPath);
String queryString = request.getQueryString();//知道
System.out.println("获取到的请求参数是:"+queryString);
String requestURI = request.getRequestURI();
System.out.println("获取到的URI是:"+requestURI);
StringBuffer requestURL = request.getRequestURL();
System.out.println("获取到的URL是:"+requestURL);
//URL:统—资源定位符号 URI:统—资源标识符,范围更广
String protocol = request.getProtocol();
System.out.println("获取到的版本信息是:"+protocol);
String remoteAddr = request.getRemoteAddr();
System.out.println("获取到的请求地址是:"+remoteAddr);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
运行结果如下:
三、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("/test02")
public class test02_Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("hhhhhhh");
//引号内为请求头的名字,获取请求头的值
String referer = request.getHeader("referer");
System.out.println("我从哪里来:"+referer);
//获取所有的请求头的名字
Enumeration<String> names = request.getHeaderNames();
while (names.hasMoreElements()){
String name = names.nextElement();
System.out.println("请求头名字是:"+name);
String value = request.getHeader(name);
System.out.println(name+":"+value);//键和值依次输出
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
运行结果如下:
四、request获取请求体
- 只用post提交方式有请求体
- 请求体把请求内容以流的方式封装起来
代码如下:
HTML代码块:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试http请求数据格式</title>
</head>
<body>
<form action="/test03" method="post">
<input type="text" placeholder="请输入用户名" name="user">
<input type="submit" value="提交">
</form>
</body>
</html>
Java代码块:
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;
//获取post请求的请求体
@WebServlet("/test03")
public class test03_Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取post请求的请求体
BufferedReader reader = request.getReader();
String s=null;
while ((s=reader.readLine())!=null){
System.out.println(s);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
运行结果如下: