Servlet—Response对象及原理
响应消息的格式:
-
响应行:
- 协议/版本 响应状态码 状态码描述
状态码:都是三位数字
- 1xx:服务器就收客户端消息,但没有接收完成,等待一段时间后发送1xx状态码
- 2xx:成功
- 3xx:重定向。代表:302(重定向),304(访问缓存)
- 4xx:客户端错误。404(请求路径错误),405(请求方式没有对应的doXxx方法)
- 5xx:服务器错误。500(服务器内部出现异常)
-
响应头
-
格式:头名称: 值
-
常见的响应头:
-
Content-Type:服务器告诉客户端本次响应体数据格式以及编码格式
-
Content-disposition:服务器告诉客户端以什么格式打开响应体数据
in-line:默认值,在当前页面内打开
attachment:以附件形式打开(文件下载中使用)
-
-
-
响应空行
-
响应体:发送的数据
Response对象:
功能:设置响应消息
-
设置响应行
- 格式:HTTP/1.1 200 ok
- 设置状态码:setStatus(int sc)
-
设置响应头
setHeader(String name,String value)
-
设置响应体
-
获取输出流
字符输出流:PrintWriter getWriter()
字节输出流:ServletOutputStream getOutputStream()
-
使用输出流,将数据输出到客户端浏览器
-
案例:
重定向:资源跳转(两种方式)
@WebServlet("/ResponseDemo1")
public class ResponseDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo1");
//第一种重定向方式
//重定向, 访问ResponseDemo1,自动跳转到ResponseDemo2
//1.设置状态码为302
response.setStatus(302);
//2.设置响应头"location" 值:跳转的资源路径
response.setHeader("location","/ResponseDemo2");
//第二种重定向方式,只需要传入重定向的路径
response.sendRedirect("/ResponseDemo2");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
@WebServlet("/ResponseDemo2")
public class ResponseDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo2");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
重定向的特点:redirect
- 地址栏发生变化
- 重定向可以访问其他站点(服务器)的资源
- 重定向是两次请求,不能使用request对象来共享数据
转发的特点:forward
- 转发地址栏路径不变
- 转发只能访问当前服务器下的资源
- 转发是一次请求,可以使用request对象来共享数据
相对路径和绝对路径
相对路径:通过相对路径不可以确定唯一资源
如:./index.html ;不以/开头,以.开头
绝对路径:通过绝对路径可以确定唯一资源
以/开头的路径
使用规则:判断定义的路径是给谁用的?也就是判断请求将从哪发出
给客户端浏览器使用:需要加虚拟目录(项目的访问路径):(标签的路径、表单路径,重定向路径)
给服务器使用:不需要加虚拟目录 (转发的路径)
服务器输出字符数据到浏览器
- 获取字节输出流
- 输出数据
//获取流对象之前,设置流的默认编码为gbk(本行可以不写)
// response.setCharacterEncoding("utf-8");
//告诉浏览器,服务器发送的消息体的数据的编码,建议浏览器使用该编码解码(设置响应头)
// response.setHeader("content-type","text/html;charset=utf-8");
//简单的形式,设置编码
response.setContentType("text/html;charset=utf-8 ");
//1. 获取字节输出流
PrintWriter pw = response.getWriter();
//2.输出数据
pw.write("你好");
服务器输出字节数据到浏览器
- 获取字节输出流
- 输出数据
//设置流的编码以及浏览器节码的编码
response.setContentType("text/html;charset=gbk");
//获取字节输出流
ServletOutputStream os = response.getOutputStream();
os.write("你好".getBytes());
验证码:
@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 graphics = image.getGraphics(); //画笔对象
graphics.setColor(Color.PINK); //设置画笔颜色
graphics.fillRect(0,0,width,height); //填充图片,从左上角画
//2.2画边框
graphics.setColor(Color.BLUE);
graphics.drawRect(0,0,width - 1,height - 1); //画边框,边框也占有像素,所以减1
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz1234567890";
//生成随机角标
Random random = new Random();
for (int i = 1; i <= 4; i++) {
int index = random.nextInt(str.length());//随机生成索引,最大值str的长度
//获取字符
char c = str.charAt(index);
//2.3 写验证码
graphics.drawString(c+"",width/5*i,height/2);
}
//2.4画干扰线
graphics.setColor(Color.GREEN);
//随机生成坐标点
for (int i = 0; i < 10; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
graphics.drawLine(x1,y1,x2,y2);
}
//3.将图片输出到页面展示
//ImageIO将内存中的图片写到任意的流当中去;参数:图片对象 后缀名 流
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}