Request是请求对象,Response是响应对象。这两个对象在我们使用Servlet的时候有看到


request:获取请求数据:
request对象,所以我们可以从request对象中获取请求的相关参数response:设置响应数据:
response对象,按照[响应行+响应头+响应体]格式拼接结果对于上述所讲的内容,我们通过一个案例来初步体验下request和response对象的使用。
结果:
小结
在这节中,我们主要认识了下request对象和reponse对象:


从上图中可以看出,ServletRequest和HttpServletRequest都是Java提供的,所以我们可以打开JavaEE提供的API文档

所以ServletRequest和HttpServletRequest是继承关系,并且两个都是接口,接口是无法创建对

这个时候,我们就需要用到Request继承体系中的RequestFacade:
该类实现了HttpServletRequest接口,也间接实现了ServletRequest接口。
对于上述结论,要想验证,可以编写一个Servlet,在方法中把request对象打印下,就能看到最终的对象是不是RequestFacade,代码如下:
@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 {
}
}
Tomcat需要解析请求数据,封装为request对象,并且创建request对象传递到service方法HTTP请求数据总共分为三部分内容,分别是**请求行、请求头、请求体**,对于这三部分内容的数据,分别该如何获取,首先我们先来学习请求行数据如何获取?

String getMethod()
String getContextPath()
StringBuffer getRequestURL()
String getRequestURI()
String getQueryString()
介绍完上述方法后,咱们通过代码把上述方法都使用下:
//String getMethod(): 获取请求方式:get
String method = req.getMethod();
System.out.println( "method:" + method);//默认GET
//Sting getContextPath();获取虚拟项目(访问路径) /request-demo
String servletPath = req.getServletPath();
System.out.println("servletPath:"+servletPath);// 默认路径为 /req1
//StringBuffer getRequestURL() 获取统一资源定位符(URL): ?之前的所有
StringBuffer requestURL = req.getRequestURL();
System.out.println("requestURL:"+requestURL);
//String getRequestURI():获取统一资源标识符 /Web-demo/.....
String requestURI = req.getRequestURI();
System.out.println("requestURI:"+requestURI);
System.out.println("------------------");
//获取请求头:user-agent 获取浏览器的版本信息
String agent = req.getHeader("user-agent");
System.out.println("agent:"+agent);
//获取请求体
//Sting getQueryString();获取请求参数Get方式的参数 类似于这些 username=zhangsan
String queryString = req.getQueryString();
System.out.println("queryString:"+queryString);
启动服务器,访问http://localhost:8080/request-demo/req1?username=zhangsan&passwrod=123,获取的结果如下:
获取请求头数据
对于请求头的数据,格式为key: value如下:

所以根据请求头名称获取对应值的方法为:
String getHeader(String name);
接下来,在代码中如果想要获取客户端浏览器的版本信息,则可以使用:
/**
* request 获取请求数据
*/
@WebServlet("/req1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String agent = req.getHeader("user-agent");//获取请求头: user-agent: 浏览器的版本信息
System.out.println(agent);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
重新启动服务器后,http://localhost:8080/request-demo/req1username=zhangsan&passwrod=123,获取的结果如下:

请求方式变更为POST**,请求体中的数据格
对于请求体中的数据,
post方式的Request对象提供了如下两种方式来获取其中的数据,分别是:
ServletInputStream getInputStream();//该方法可以获取字节
BufferedReader getReader();//该方法可以获取字符串
接下来,要想获取到请求体的内容该如何实现?
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/request-demo/req1" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
form>
body>
html>
@WebServlet(urlPatterns="/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 {
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取post请求体:请求参数
//1. 获取字符输入流
BufferedReader br = req.getReader(); //所得参数是字符串则调用getReader();
//2. 读取数据
String line = br.readLine();
System.out.println(line);
//在post方式中是不用关闭br.close()的, post方式随着项目停止而停止
/* 方法 2
ServletInputStream is = req.getInputStream();
int len=0;
byte[] b = new byte[1024];
while ((len = is.read(b))!=-1){
System.out.println(new String(b,0,len));
}*/
}
注意


小结:
HTTP请求数据中包含了请求行、请求头和请求体,针对这三部分内容,Request对象都提供了对应的
API方法来获取对应的值:


当然,也可以在doGet中调用doPost,在doPost中完成参数的获取和打印,另外需要注意的是,doGet和doPost方法都必须存在,不能删除任意一个。
GET请求和POST请求获取请求参数的方式不一样,在获取请求参数这块该如何实现呢?
解决方案一:
使用request的getMethod()来获取请求方式,根据请求方式的不同分别获取请求参数值,这样就可
以解决上述问题,但是以后每个Servlet都需要这样写代码,实现起来比较麻烦,这种方案我们不采
用
@WebServlet("/req1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求方式
String method = req.getMethod();
//获取请求参数
String params = "";
if("GET".equals(method)){
params = req.getQueryString();
}else if("POST".equals(method)){
BufferedReader reader = req.getReader();
params = reader.readLine();
}
//将请求参数进行打印控制台
System.out.println(params);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
解决方案二:
request对象已经将上述获取请求参数的方法进行了封装,并且request提供的方法实现的功能更强
大,以后只需要调用request提供的方法即可,在request的方法中都实现了哪些操作?
根据不同的请求方式获取请求参数,获取的内容如下:

把获取到的内容进行分割,内容如下:

把分割后端数据,存入到一个Map集合中:

注意:因为参数的值可能是一个,也可能有多个,所以Map的值的类型为String数组。
基于上述理论,request对象为我们提供了如下方法:
获取所有参数Map集合
java Map
根据名称获取参数值(数组)
java String[] getParameterValues(String name);
根据名称获取参数值 (单个值)
java String getParameter(String name);
接下来,我们通过案例来把上述的三个方法进行实例演示:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/Web_demo_war/req1" method="get">
<lable for="username">账号:lable>
<input type="text" name="username">
<lable for="password">密码:lable>
<input type="password" name="password">
<br>
<input type="checkBox" name="hobby" value="1">唱
<input type="checkBox" name="hobby" value="2">跳
<br/>
<input type="submit" name="submit" value="提交">
form>
body>
html>

package com.sgs.web.RequestDemo;
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("/req1")
public class RequestDemo2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* // 方式1 获取所有参数Map集合 Map getParameterMap()
Map map = request.getParameterMap();
for (String key : map.keySet()) {
//获取map的键
System.out.print(key+":");
//根据键返回值
for (String value:map.get(key)) {
System.out.print(value+" ");
}
}
System.out.println();
System.out.print("-----------------------");
System.out.println();
// 方式2 根据名称获取参数值(数组) String[] getParameterValues(String name)
//这里我们将复选框的值采取出来
String[] hobbies = request.getParameterValues("hobby");
System.out.print("hobby:");
for (String value:hobbies){
System.out.print(" "+value);
}
System.out.println();
System.out.print("-----------------------");
System.out.println();*/
// 方式3 经常用 根据名称获取参数值(单个值!) String getParameter(String name)
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: "+username);
System.out.println("password: "+password);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
结果图:

小结

使用通用方式获取请求参数后,屏蔽了GET和POST的请求方式代码的不同,则代码可以定义如下格式

由于格式固定,所以我们可以使用IDEA提供的模板来制作一个Servlet的模板,这样我们后期在创建
Servlet的时候就会更高效,具体如何实现:


问题展示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/Web_demo_war/req2" method="get">
<lable for="username">账号:</lable>
<input type="text" name="username">
<lable for="password">密码:</lable>
<input type="password" name="password">
<br>
<input type="checkBox" name="hobby" value="1">游泳
<input type="checkBox" name="hobby" value="2">爬山
<br/>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
package com.sgs.web.RequestDemo;
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("/req2")
public class RequestDemo2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* // 方式1 获取所有参数Map集合 Map getParameterMap()
Map map = request.getParameterMap();
for (String key : map.keySet()) {
//获取map的键
System.out.print(key+":");
//根据键返回值
for (String value:map.get(key)) {
System.out.print(value+" ");
}
}
System.out.println();
System.out.print("-----------------------");
System.out.println();
// 方式2 根据名称获取参数值(数组) String[] getParameterValues(String name)
//这里我们将复选框的值采取出来
String[] hobbies = request.getParameterValues("hobby");
System.out.print("hobby:");
for (String value:hobbies){
System.out.print(" "+value);
}
System.out.println();
System.out.print("-----------------------");
System.out.println();*/
// 方式3 经常用 根据名称获取参数值(单个值!) String getParameter(String name)
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: "+username);
System.out.println("password: "+password);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
启动服务器,页面上输入中文参数

查看控制台打印内容

把req.html页面的请求方式改成post,再次发送请求和中文参数

查看控制台打印内容,依然为乱码

通过上面的案例,会发现,不管是GET还是POST请求,在发送的请求参数中如果有中文,在后台接收 的时候,都会出现中文乱码的问题。具体该如何解决呢?
分析出现中文乱码的原因:
request的getReader()或者是getInputStream()来获取流中的数据解决方案:
页面设置的编码格式为UTF-8把TOMCAT在获取流数据之前的编码设置为UTF-8setCharacterEncoding("UTF-8")设置编码,UTF-8也可以写成小写修改后的代码为:
package com.sgs.web.RequestDemo;
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("/req2")
public class RequestDemo2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//这是post请求方式解决中文乱码问题
request.setCharacterEncoding("UTF-8");
/* // 方式1 获取所有参数Map集合 Map getParameterMap()
Map map = request.getParameterMap();
for (String key : map.keySet()) {
//获取map的键
System.out.print(key+":");
//根据键返回值
for (String value:map.get(key)) {
System.out.print(value+" ");
}
}
System.out.println();
System.out.print("-----------------------");
System.out.println();
// 方式2 根据名称获取参数值(数组) String[] getParameterValues(String name)
//这里我们将复选框的值采取出来
String[] hobbies = request.getParameterValues("hobby");
System.out.print("hobby:");
for (String value:hobbies){
System.out.print(" "+value);
}
System.out.println();
System.out.print("-----------------------");
System.out.println();*/
// 方式3 经常用 根据名称获取参数值(单个值!) String getParameter(String name)
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: "+username);
System.out.println("password: "+password);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
至此POST请求中文乱码的问题就已经解决,
但是这种方案不适用于GET请求,这个原因是什么呢,咱
们下面再分析。
(Post请求)如图所示:首先我们需要先分析下
GET请求出现乱码的原因

在进行URL编码的时候会采用页面标签指定的UTF-8的方式进行编码,张三编码后的结果后台服务器(Tomcat)接收到%E5%BC%A0%E4%B8%89后会默认按照ISO-8859-1进行URL 解码思考: 如果把req.html页面的标签的charset属性改成ISO-8859-1 ,后台不做操作,能解
决中文乱码问题么?
是否定的,因为ISO-8859-1本身是不支持中文展示的,所以改了标签的charset属性后,会导分析完上面的问题后,我们会发现,其中有两个我们不熟悉的内容就是
URL编码和URL解码,什么是
URL编码,什么又是URL解码呢?
张三按照UTF-8的方式转换成二进制的结果为:1 1110 0101 1011 1100 1010 0000 1110 0100 1011 1000 1000 1001
这个结果是如何计算的?
使用http://www.mytju.com/classcode/tools/encode_utf8.asp,输入张三

就可以获取张和三分别对应的10进制,然后在使用计算器,选择程序员模式,计算出对应的二进制数
据结果:

在计算的十六进制结果中,每两位前面加一个%,就可以获取到%E5%BC%A0%E4%B8%89。
当然你从上面所提供的网站中就已经能看到编码16进制的结果了:

但是对于上面的计算过程,如果没有工具,纯手工计算的话,相对来说还是比较复杂的,我们也不需
要进行手动计算
在Java中已经为我们提供了编码和解码的API工具类(net包下的)可以让我们更快速的进行编码和解码:
编码:
java.net.URLEncoder.encode("需要被编码的内容","字符集(UTF-8)")
解码:
java.net.URLDecoder.decode("需要被解码的内容","字符集(UTF-8)")
接下来咱们对张三来进行编码和解码:
public class URLDemo {
public static void main(String[] args) throws UnsupportedEncodingException{
String username = "张三";
//1. URL编码
String encode = URLEncoder.encode(username, "utf-8");
System.out.println(encode); //打印:%E5%BC%A0%E4%B8%89
//2. URL解码
//String decode = URLDecoder.decode(encode, "utf-8");//打印:张三
String decode = URLDecoder.decode(encode, "ISO-8859-1");//打印:`å¼ ä¸ `
System.out.println(decode);
}
}
到这,我们就可以分析出GET请求中文参数出现乱码的原因了
浏览器把中文参数按照UTF-8进行URL编码Tomcat对获取到的内容进行了ISO-8859-1的URL解码,在控制台就会出现类上å¼ ä口最后一位是个空格
为所以我们可以考虑把å¼ ä¸口转换成字节,
在把字节转换成张三,在转换的过程中是它们的编码 一致,就可以解决中文乱码问题。
具体的实现步骤为:
public class URLDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String username = "张三";
//1. URL编码
String encode = URLEncoder.encode(username, "utf-8");
System.out.println(encode);
//2. URL解码
String decode = URLDecoder.decode(encode, "ISO-8859-1");
//本来是"UTF-8,在这里讲解采用"ISO-8859-1"来说,出现乱码是故意的,为了让大家看下面String的解码过程也是可以的
System.out.println(decode); //此处打印的是对应的乱码数据
//3. 转换为字节数据,编码
byte[] bytes = decode.getBytes("ISO-8859-1");
for (byte b : bytes) {
System.out.print(b + " ");
}
//此处打印的是:-27 -68 -96 -28 -72 -119
//4. 将字节数组转为字符串,解码
String s = new String(bytes, "utf-8");
System.out.println(s); //此处打印的是张三
}
}
说明:在第18行中打印的数据是-27 -68 -96 -28 -72 -119和张三转换成的二进制数据1110 0101
1011 1100 1010 0000 1110 0100 1011 1000 1000 1001为什么不一样呢?
其实打印出来的是十进制数据,我们只需要使用计算机换算下就能得到他们的对应关系,如下图:

至此对于GET请求中文乱码的解决方案,我们就已经分析完了,最后在代码中去实现。
小结
中文乱码解决方案
POST请求解决方案是:设置输入流的编码request.setCharacterEncoding("UTF-8");//注意:设置的字符集要和页面保持一致
通用方式(GET/POST):需要先解码,再编码new String(username.getBytes("ISO-8859-1"),"UTF-8");
URL编码实现方式:
URLEncoder.encode(str,"UTF-8");// -----> 得到的对象是 s
URLDecoder.decode(s,"UTF-8");//通过该对象 s 进行UTF-8的解码
注意
另外需要说明一点的是Tomcat8.0之后,已将GET请求乱码问题解决,设置默认的解码方式为UTF-8