文章目录
1.简介
-
request和response对象是由服务器创建的,我们来使用它们
-
request对象是来获取请求消息,response对象是来响应消息
2.继承体系结构
3.Request功能
1.获取请求消息数据
获取请求行数据
请求方式
请求url
请求协议/版本
GET
/ServletDemo/Demo?uername=Mr.Q
HTTP/1.1
/ServletDemo
虚拟目录 /Demo
Servlet路径 uername=Mr.Q
请求参数
方法:
-
获取请求方式:GET
String getMethod()
-
获取虚拟目录:/ServletDemo
String getContextPath()
-
获取Servlet路径:/Demo
String getservletPath()
-
获取get方式请求参数:username=Mr.Q
String getQueryString()
-
获取请求URI :/ServletDemo/Demo
String getRequestURI()
:/ServletDemo/DemoStringBuffer getRequestURL()
:http://localhost/ServletDemo/Demo
-
URL:统一资源定位符 /ServletDemo/Demo
-
URI:统一资源标识符 http://localhost/ServletDemo/Demo
URI 的范围要大于URL
举个例子:
URI相当于-----共和国
URL相当于-----中华人名共和国
-
获取协议及版本:HTTP / 1.1
String getprotocol()
-
获取客户机的IP地址
String getRemoteAddr()
获取请求行数据LineDemo
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;
/**
* @Author: Mr.Q
* @Date: 2020-02-05 15:38
* @Description:Request对象获取请求行数据
* 1.获取请求方式:GET---String getMethod()
* 2.获取虚拟目录:/ServletDemo---String getContextPath()
* 3.获取Servlet路径:/Demo---String getServletPath()
* 4.获取get方式请求参数:username=Mr.Q---String getQuerystring()
* 5.获取请求URI: /ServletDemo/Demo
* String getRequestURI(): /ServletDemo/Demo
* StringBuffer getRequestURL(): http://1ocalhost/ServletDemo/Demo
* 6.获取协议及版本: HTTP/1.1---String getProtocol()
* 7.获取客户机的IP地址---String getRemoteAddr()
*/
@WebServlet("/Servlet_RequestLine")
public class Servlet_RequestLine extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求方式:GET---String getMethod()
String method = request.getMethod();
System.out.println("获取请求方式GET: " + method);
//2.获取虚拟目录:/ServletDemo---String getcontextPath()
String contextPath = request.getContextPath();
System.out.println("获取虚拟目录: " + contextPath);
//3.获取Servlet路径:/Demo---String getServletPath()
String servletPath = request.getServletPath();
System.out.println("获取Servlet路径:" + request.getServletPath());
//4.获取get方式请求参数:username=Mr.Q---String getQueryString()
String queryString = request.getQueryString();
System.out.println("获取get方式请求参数: " + queryString);
//5.获取请求URI
String requestURI = request.getRequestURI();
System.out.println("getRequestURI(): " + requestURI);
StringBuffer requestURL = request.getRequestURL();
System.out.println("getRequestURL: " + requestURL);
//6.获取协议及版本: HTTP/1.1---String getProtocol()
String protocol = request.getProtocol();
System.out.println("获取协议及版本: " + protocol);
//7.获取客户机的IP地址---String getRemoteAddr()
String remoteAddr = request.getRemoteAddr();
System.out.println("获取客户机的IP地址: " + remoteAddr);
}
}
获取请求头数据
方法:
String getHeader(String name)
:通过请求头来获取对应的请求头的数据
Enumeration<String>getHeaderNames()
:获取所有的请求头名称及数据
获取请求头数据HeaderDemo
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;
/**
* @Author: Mr.Q
* @Date: 2020-02-05 16:29
* @Description:Request对象获取请求头数据
*/
@WebServlet("/Servlet_RequestHead")
public class Servlet_RequestHead extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
//2.遍历
while(headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String value = request.getHeader(name);
System.out.println(name + "----" + value);
}
//获取请求头数据user-agent
//String agent = request.getHeader("user-agent");
//if(agent.contains("Chrome")) { } else { }
//获取请求头数据referer
//String agent = request.getHeader("referer");
}
}
获取请求体数据
请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
步骤:
-
获取流对象
-
BufferedReader getReader():获取字符输入流,只能操作字符数据
-
ServletInputstream getInputstream():获取字节输入流,可以操作所有类型数据
-
-
再从流对象中数据
RequestBody
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求消息体
//1.获取字符流
BufferedReader br = request.getReader();
//2.读取数据
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
regist.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<form action="/RequestBody" method="post">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
启动Tomcat,访问regist.html,提交表单
Post请求:
请求空行为空数据,起分隔数据的作用,没有必要获取
2.其他功能
中文乱码问题
产生原因:编解码不一致造成。doPost()
为默认GBK编码,而访问的HTML页面为UTF-8
编码
解决方法:
- Get方式:Tomcat8已经将Get乱码问题解决3
- Post方式:在获取参数前,设置request的编码
request.setCharacterEncoding("UTF-8");
1.获取请求参数通用方式
Get或者Post请求方式都可以使用下列方法来获取请求参数
String getParameter(String name)
:根据参数名称获取参数值
String[] getParameterValues(String name)
:根据参数名称获取参数值的数组 (用于复选框)
Enumeration<string> getParameterNames()
:获取所有请求的参数名称
Map<String,String[]> getParameterMap()
:获取所有参数的map集合
PG_getParameterMap.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.IOException;
import java.util.Map;
import java.util.Set;
/**
* @Author: Mr.Q
* @Date: 2020-02-07 10:19
* @Description:
*/
@WebServlet("/PG_getParameterMap")
public class PG_getParameterMap extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String[]> parameterMap = request.getParameterMap();
//遍历
Set<String> setkey = parameterMap.keySet();
for (String name : setkey) {
//获取键,获取值
String[] values = parameterMap.get(name);
System.out.println(name);
for (String value : values) {
System.out.println(value);
}
System.out.println("--------------------------");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<form action="/PG_getParameterMap" method="post">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="Hobby" value="game">游戏
<input type="checkbox" name="Hobby" value="study">学习
<input type="checkbox" name="Hobby" value="code">Coding<br><br>
<input type="submit" value="注册">
</form>
</body>
</html>
启动Tomcat,访问regist.html,提交表单
2.请求转发
请求转发:一种在服务器内部的资源跳转方式
步骤:
-
通过request对象获取请求转发器对象: RequestDispatcher getRequestDispatcher(String path)
-
使用RequestDispatcher对象来进行转发:forward(ServletRequest request,ServletResponse response)
request.getRequestDispatcher("/ServletB").forward(request, response);
特点:
-
浏览器地址栏路径不发生变化
-
只能转发到当前服务器内部资源中
-
转发是一次请求(请求范围是在一个request域中)
3.共享数据
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据,只能用于请求转发
方法:
-
void setAttribute(string name,Object obj):存储数据
-
Object getAttitude(string name):通过键获取值
-
void removeAttribute(string name):通过键移除键值对
4.获取ServletContext
ServletContext servletContext = request.getServletContext();