HTTP协议之response对象

在这里插入图片描述

功能

设置响应消息

1. 设置响应行
        1. 格式:HTTP/1.1  200    ok
        2. 设置状态码: setStatic(int sc)
2. 设置相应头
    setHeader(String name,String value)
3. 设置响应体
   使用步骤:
           1. 获取输出流
                   * 字符输出流:PrintWriter getWriter()
                   * 字节输出流: ServletOutputStream getOutStream()
           2. 使用输出流,将数据输出到客户端浏览器

案例

完成重定向

资源跳转的方式。

重定向

代码实现:

这是第一个访问的Servlet (/rd1)
在这里插入图片描述
这是第二个Servlet (/rd2) 重定向来的

这是运行结果:

特点

转发特点:foward
    1. 转发地址栏路径不变
    2. 转发只能访问当前服务器下的资源
    3. 转发是一次请求,可以使用request对象来共享数据

重定向特点:redirect
    1. 转发地址栏路径发生变化
    2. 转发可以访问其他站点(服务器)的资源
    3. 转发是两次请求,不能使用request来共享数据

路径的写法

  1. 路径的分类

    1. 相对路径

      不能确定唯一资源
      如:./index.html
      
      规则:找到当前资源和目标资源的相对位置关系
          ./:当前目录
         ../:后退一级目录
    2. 绝对路径

      可以确定唯一的资源
      *如:http://localhost:8080/3_response_war_exploded/rd2
      *以/开头:/3_response_war_exploded/rd2
      
      规则:判断路径给谁用,判断请求从哪儿发出
          *给客户端使用:需要加虚拟目录(项目的访问路径)
             *建议虚拟目录动态获取:request.getContextPath()
             *<a>,<form> 重定向...
          *给服务器使用,不需要加虚拟目录
            * 转发路径

服务器输出字符数据到浏览器

步骤

  1. 获取字符输出流
  2. 输出数据
  3. 注意:
    乱码问题:
1. PrintWriter pw = response.getWriter();
2. 设置该流的默认编码
3. 告诉浏览器,响应体用的编码
1
2
	//简单的形式,设置编码,在获取响应流之前设置
response.setContentType("text/html;charset=UTF-8");
  1. 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.web.servlet;

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.io.PrintWriter;

@WebServlet(urlPatterns = "/rd3")
public class ResponseDemo3 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取流对象之前,设置流的默认编码:ISO -8859-1 设置为GBK
response.setContentType("text/html;charset=UTF-8");

//1.获取字符输出流
PrintWriter pw = response.getWriter();

//2.输出数据
pw.write("<h1>hello response<h1>");
pw.write("你好 response");
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

服务器输出字节数据到浏览器

步骤:

1.  获取字节输出流
2.  输出数据

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.web.servlet;

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;

/**
* @author OldAZ-PC
*/
@WebServlet(urlPatterns = "/rd4")
public class ResponseDemo4 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取流对象之前,设置流的默认编码:ISO -8859-1 设置为GBK
response.setContentType("text/html;charset=UTF-8");


//1.获取字节输出流
ServletOutputStream sos = response.getOutputStream();


//2.输出数据
sos.write("hello".getBytes());

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

验证码

1. 本质:图片
2. 目的:防止恶意表单注册
      注:以后写的话  ,几乎都是在网上找美观的代码。自己改!!!

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.web.servlet;

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.util.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
* @author OldAZ-PC
*/
@WebServlet(urlPatterns = "/rd5")
public class ResponseDemo5 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取流对象之前,设置流的默认编码:ISO -8859-1 设置为GBK
// response.setContentType("text/html;charset=UTF-8");

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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//生成随机角标
Random ran = new Random();
for (int i = 1; i <5 ; 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());

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

HTML页面代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img id="checkCode" src="/3_response_war_exploded/rd5"/>
<a id="change" herf="">看不清,换一张?</a>
</body>
<script>
/*
分析:
点击超链接或者图片,需要换一张
1. 给超链接或者图片绑定单击事件
2.重新设置图片的SRC属性值
*/
window.onload = function () {
//1.获取图片对象
var img = document.getElementById("checkCode");
img.onclick=function () {
//时间戳
var date = new Date().getTime();

img.src = "/3_response_war_exploded/rd5?"+date;
}



var img2 = document.getElementById("change");
img2.onclick = function () {
//时间戳
var date = new Date().getTime();

img.src = "/3_response_war_exploded/rd5?"+date;
}


}


</script>
</html>
文章目录
  1. 1. 功能
  2. 2. 案例
    1. 2.1. 完成重定向
      1. 2.1.1. 资源跳转的方式。
      2. 2.1.2. 代码实现:
      3. 2.1.3. 特点
    2. 2.2. 路径的写法
    3. 2.3. 服务器输出字符数据到浏览器
      1. 2.3.1. 步骤
    4. 2.4. 服务器输出字节数据到浏览器
    5. 2.5. 验证码
,