Response对象
- 功能:设置响应消息 1. 设置响应行 1. 格式:HTTP/1.1 200 ok 2. 设置状态码:setStatus(int sc) 2. 设置响应头:setHeader(String name, String value) 3. 设置响应体: * 使用步骤: 1. 获取输出流 * 字符输出流:PrintWriter getWriter() * 字节输出流:ServletOutputStream getOutputStream() 2. 使用输出流,将数据输出到客户端浏览器
- 完成重定向
重定向:资源跳转的方式
代码语言:javascript复制* 代码实现:
//简单的重定向方法
response.sendRedirect("/day15/responseDemo2");
* forward 和 redirect 区别
* 重定向的特点:redirect(客户端使用)
1. 地址栏发生变化
2. 重定向可以访问其他站点(服务器)的资源
3. 重定向是两次请求。不能使用request对象来共享数据
* 转发的特点:forward(服务器端使用)
1. 转发地址栏路径不变
2. 转发只能访问当前服务器下的资源
3. 转发是一次请求,可以使用request对象来共享数据
代码示例如下 1. servlet1.java(发起重定向)
代码语言:javascript复制package servlet_response;/*
*Created by tao on 2020-03-17.
*/
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("/response1")
public class servlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是response1");
request.setAttribute("msg", "request对象共享数据");
//动态获取虚拟目录
String contextPath = request.getContextPath();
System.out.println(contextPath);
//简单的重定向方法
response.sendRedirect(contextPath "/response2");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
- servlet2.java(重定向接收,并且发起一个转发)
package servlet_response;/*
*Created by tao on 2020-03-17.
*/
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("/response2")
public class servlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是response2");
Object msg = request.getAttribute("msg");
//重定向是两次请求,获取不到request对象共享数据
System.out.println("获取到的数据:" msg);
//转发
request.setAttribute("msg", "request对象共享数据");
request.getRequestDispatcher("/response3").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
- servlet3.java(转发接收)
package servlet_response;/*
*Created by tao on 2020-03-17.
*/
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("/response3")
public class servlet3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是response3");
Object msg = request.getAttribute("msg");
//转发是一次请求,可以获取到request对象共享数据
//转发地址栏路径不变
System.out.println("获取到的数据:" msg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
实现效果如下
验证了上述所说的重定向和转发的区别
向客户端响应数据(字符流和字节流)
代码语言:javascript复制1. 服务器输出字符数据到浏览器
* 步骤:
1. 获取字符输出流
2. 输出数据
* 注意:
* 乱码问题:
1. PrintWriter pw = response.getWriter();获取的流的默认编码是ISO-8859-1
2. 设置该流的默认编码
3. 告诉浏览器响应体使用的编码
//简单的形式,设置编码,是在获取流之前设置
response.setContentType("text/html;charset=utf-8");
2. 服务器输出字节数据到浏览器
* 步骤:
1. 获取字节输出流
2. 输出数据
实现代码如下 servlet4.java
代码语言:javascript复制package servlet_response;/*
*Created by tao on 2020-03-17.
*/
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
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.io.PrintWriter;
//向客户端响应数据
@WebServlet("/response4")
public class servlet4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应的编码格式
response.setContentType("text/html;charset=utf-8");
/*字符流传输*/
//1.获取字符输出流
PrintWriter pw = response.getWriter();
//2.输出数据
//pw.write("<h2>hello response</h2>");
pw.write("我是字符流传输 response");
//简写方式
response.getWriter().write("<h2>hello response</h2>");
/*字节流传输*/
//1.获取字节输出流
ServletOutputStream sos = response.getOutputStream();
//2.输出数据
sos.write("你好,我是字节流传输".getBytes("utf-8"));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
实现效果如下
响应验证码图片案例
代码语言:javascript复制验证码
1. 本质:图片
2. 目的:防止恶意表单注册
3. 方式:通过response对象,使用字节流输出图片
实现代码如下
代码语言:javascript复制package servlet_response;
import javax.imageio.ImageIO;
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.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
/*
* 字节流输出:验证码图片
* */
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 100;
int height = 50;
//1.创建一对象,在内存中图片(验证码图片对象)
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//2.美化图片
//2.1 填充背景色
Graphics g = image.getGraphics();//画笔对象
g.setColor(Color.PINK);//设置画笔颜色
g.fillRect(0,0,width,height);
//2.2画边框
g.setColor(Color.BLUE);
g.drawRect(0,0,width - 1,height - 1);
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
//生成随机角标
Random ran = new Random();
for (int i = 1; i <= 4; i ) {
int index = ran.nextInt(str.length());
//获取字符
char ch = str.charAt(index);//随机字符
//2.3写验证码
g.drawString(ch "",width/5*i,height/2);
}
//2.4画干扰线
g.setColor(Color.GREEN);
//随机生成坐标点
for (int i = 0; i < 10; i ) {
int x1 = ran.nextInt(width);
int x2 = ran.nextInt(width);
int y1 = ran.nextInt(height);
int y2 = ran.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
//3.将图片输出到页面展示
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
实现效果如下
刷新会重置验证码