Request
Github:request_demo
一. request 对象的原理
1.request 对象由服务器创建,被程序员使用。
2.request 对象用来获取请求消息。
二. request 对象继承体系结构
ServletRequest(接口)— HttpServletRequest(接口)— org.apache.catalina.connector.RequestFacade(类)
三. request 获取请求消息数据功能
1. 获取请求行数据
GET /servlet_demo/demo1?username=hjplz HTTP/1.1
1.获取请求方式:GET
String getMethod()
2.获取虚拟目录:/servlet_demo
String getContextPath()
3.获取 Servlet 路径:/demo1
String getServletPath()
4.获取 GET 方式请求参数:username=hjplz
String getQueryString()
5.获取请求URI:/servlet_demo/demo1
- URI:统一资源标识符
String getRequestURI()
:/servlet_demo/demo1- URL:统一资源定位符
StringBuffer getRequestURL()
:http://localhost/servlet_demo/demo1
6.获取协议及版本:HTTP/1.1
String getProtocol()
7.获取客户机的IP地址
String getRemoteAddr()
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;
/**
* 演示 Request对象 获取请求行数据
*/
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求方式:GET
String method = request.getMethod();
System.out.println(method);
// (*)获取虚拟目录:/servlet_demo
String contextPath = request.getContextPath();
System.out.println(contextPath);
// 获取 Servlet 路径:/demo1
String servletPath = request.getServletPath();
System.out.println(servletPath);
// 获取 GET 方式请求参数:username=hjplz
String queryString = request.getQueryString();
System.out.println(queryString);
// (*)获取请求 URI:/servlet_demo/demo1
String requestURI = request.getRequestURI();
System.out.println(requestURI);
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURL);
// 获取协议及版本:HTTP/1.1
String protocol = request.getProtocol();
System.out.println(protocol);
// 获取客户机的 IP 地址
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}
2. 获取请求头数据
1.String getHeader(String name)
:通过请求头的名称获取请求头的值
2.Enumeration<String> getHeaderNames()
:获取所有请求头的名称
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;
/**
* 演示 Request 对象获取请求头数据
*/
@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
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);
}
}
}
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;
/**
* 演示 Request 对象获取请求头数据:user-agent
*/
@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String agent = request.getHeader("user-agent");
// 判断 agent 的浏览器版本
if(agent.contains("Chrome")) {
System.out.println("Chrome...");
} else if(agent.contains("Firefox")) {
System.out.println("Firefox...");
}
}
}
package com.hjplz.web.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;
/**
* 演示 Request 对象获取请求头数据:referer
*/
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String referer = request.getHeader("referer");
System.out.println(referer);
// 防盗链
if(referer != null) {
if(referer.contains("login")) {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("正常访问...");
} else {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("盗链!");
}
}
}
}
3. 获取请求体数据
1.获取输入流
- 字符输入流:
BufferedReader getReader()
- 字节输入流:
ServletInputStream getInputStream()
2.从输入流中获取数据
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("/requestDemo5")
public class RequestDemo5 extends HttpServlet {
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);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
四. request 其他功能
1. 获取请求参数的通用方式
1.String getParameter(String name)
:根据参数名称获取参数值。e.g. 参数为 username=hjplz&password=123
2.String[] getParameterValues(String name)
:根据参数名称获取参数值的数组。e.g. 参数为 hobby=study&hobby=game
3.Enumeration<String> getParameterNames()
:获取所有请求的参数名称
4.Map<String, String[]> getParameterMap()
:获取所有参数的 map 集合
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 {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println(username);
// 根据参数名称获取参数值的数组
String[] hobbies = request.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
// 获取所有请求的参数名称
Enumeration<String> parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()) {
String name = parameterNames.nextElement();
System.out.println(name);
// 一个键只对应一个值
// String value = request.getParameter(name);
// System.out.println(value);
// 一个键可能对应多个值
String[] values = request.getParameterValues(name);
for (String value : values) {
System.out.println(value);
}
System.out.println("-----------");
}
// 获取所有参数的 map 集合
Map<String, String[]> parameterMap = request.getParameterMap();
// 获取键的集合
Set<String> keyset = parameterMap.keySet();
// 根据键获取值
for (String name : keyset) {
String[] values = parameterMap.get(name);
System.out.println(name);
for (String value : values) {
System.out.println(value);
}
System.out.println("----------");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
5.中文乱码
- GET:Tomcat 8 已经将 GET 方式的乱码问题解决
- POST:需要在获取参数之前设置 request 的编码:
request.setCharacterEncoding("UTF-8");
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("/requestDemo7")
public class RequestDemo7 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置流的编码
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
2. 请求转发
一种服务器内部的资源跳转方式。
1.步骤
- 通过 request 对象获取请求转发器对象:
RequestDispatcher getRequestDispatcher(String path)
- 使用 RequestDispatcher 对象进行转发:
forward(ServletRequest request, ServletResponse response)
2.特点
- 浏览器地址栏路径没有发生变化
- 只能转发到当前服务器的内部资源中
- 转发在一次请求中进行
3. 共享数据
1.域对象:一个有作用范围的对象,可以在范围内共享数据。
2.request 域:一次请求的范围,一般用于在请求转发的多个资源中共享数据。
setAttribute(String name, Object obj)
:存储数据Object getAttribute(String name)
:通过键获取值removeAttribute(String name)
:通过键移除键值对
import javax.servlet.RequestDispatcher;
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;
/**
* request 对象在 request 域内共享数据
*/
@WebServlet("/requestDemo8")
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("RequestDemo8 被访问了...");
// 存储数据到 request 域
request.setAttribute("msg", "hello");
// 转发到 /resquestDemo9
request.getRequestDispatcher("/requestDemo9").forward(request, response);
// 请求转发只能转发到当前服务器的内部资源中,以下会报错
// request.getRequestDispatcher("https://baidu.com").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
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;
/**
* request 对象在 request 域内共享数据
*/
@WebServlet("/requestDemo9")
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("RequestDemo9 被访问了...");
// 获取数据
Object msg = request.getAttribute("msg");
System.out.println(msg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
3.获取 ServletContext
ServletContext getServletContext()
import javax.servlet.ServletContext;
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;
/**
* 获取 ServletContext
*/
@WebServlet("/requestDemo10")
public class RequestDemo10 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();
System.out.println(servletContext);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}