1. 响应对象(Response)
1.1 响应对象概述
- 响应,它表示了服务器端收到请求,同时也已经处理完成,把处理的结果告知用户。简单来说,指的就是服务器把请求的处理结果告知客户端。在B/S架构中,响应就是把结果带回浏览器。
- 响应对象,顾名思义就是用于在JavaWeb工程中实现上述功能的对象。
- 常用响应对象
- 响应对象也是是Servlet规范中定义的,它包括了协议无关的和协议相关的。
- 协议无关的对象标准是:ServletResponse接口
- 协议相关的对象标准是:HttpServletResponse接口
- 类结构图如下:
- 我们之前的作品中涉及的响应对象都是和HTTP协议相关的。即使用的是HttpServletResponse接口的实现类。
- 我们在使用Servlet时,需要定义一个类,然后实现Servlet接口(或者继承它的实现类)。现在我们想要实现响应功能,要不要定义一个类,然后实现HttpServletResponse接口呢?我们无需这么做。我们只需要在自己写的Servlet中直接使用即可,因为这个对象的实现类是由Tomcat提供的,无须我们自定义。同时它还会帮我们把对象创建出来并传入doGet和doPost方法中。
1.2 常用方法介绍
- 在HttpServletResponse接口中提供了很多方法,接下来我们通过API文档,来了解一下这些方法。
- 状态码
- 常用状态码:
状态码 |
说明 |
200 |
执行成功 |
302 |
它和307一样,都是用于重定向的状态码。只是307目前已不再使用 |
304 |
请求资源未改变,使用缓存。 |
400 |
请求错误。最常见的就是请求参数有问题 |
404 |
请求资源未找到 |
405 |
请求方式不被支持 |
500 |
服务器运行内部错误 |
状态码 |
说明 |
1xx |
消息 |
2xx |
成功 |
3xx |
重定向 |
4xx |
客户端错误 |
5xx |
服务器错误 |
1.3 响应对象的使用示例
1.3.1 响应-字节流输出中文问题
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str = "字节流输出中文的乱码问题";
ServletOutputStream sos = response.getOutputStream();
response.setContentType("text/html;charset=UTF-8");
sos.write(str.getBytes("UTF-8"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
1.3.2 响应-字符流输出中文问题
public class ResponseDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str = "字符流输出中文乱码";
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(str);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
1.3.3 响应-生成验证码
public class ResponseDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 200;
int height = 35;
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.BLUE);
g.drawRect(0, 0, width, height);
g.setColor(Color.GRAY);
g.fillRect(1, 1, width-2, height-2);
g.setColor(Color.WHITE);
Random r = new Random();
for(int i=0;i<10;i++){
g.drawLine(r.nextInt(width), r.nextInt(height),r.nextInt(width), r.nextInt(height));
}
g.setColor(Color.RED);
Font font = new Font("宋体", Font.BOLD,30);
g.setFont(font);
int x = 35;
for(int i=0;i<4;i++){
g.drawString(String.valueOf(r.nextInt(10)), x, 25);
x+=35;
}
ImageIO.write(image, "jpg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
1.3.4 设置响应消息头-控制缓存
void setHeader(String name, String value)
public class ResponseDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str = "设置缓存时间";
response.setDateHeader("Expires",System.currentTimeMillis()+1*60*60*1000);
response.setContentType("text/html;charset=UTF-8");
response.getOutputStream().write(str.getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}