响应消息
- 响应行
- 组成:协议/版本 响应状态码 状态码描述
- 响应状态码:服务器告诉客户端浏览器本次请求和响应的一个状态(3位数字)
- 分类:
1XX
服务器已接收用户消息,但没有完成接收,等待一段时间后,发送1xx状态码
2XX
成功(200)
3XX
重定向,代表302(重定向),304(访问缓存)
4XX
客户端错误,代表404 请求路径没有对应的资源
405 请求方式没有对应的doxxx()方法
5XX
服务端错误,代表500(服务器内部出现异常) - 响应头
常见的响应头
Content-Type
服务器告诉客户端本次响应体数据格式及编码格式 “text/html”
Content-disposition
服务器告诉客户端以什么格式打开响应体数据 值:in-line
默认值,在当前页面打开,attachment
;filename
=xxx:以附件形式打开响应体 - 响应空行
- 响应体:传输的数据
Response
功能:设置响应消息
-
设置响应行
格式 HTTP/1.1 200 OK
设置状态码:setStatus(int sc)
-
设置响应头
setHeader(String name,String value)
-
设置响应体
⭐️使用步骤
1.获取输出流
字符输出流PrintWriter getWriter()
字节输出流ServletOutputStream getOutputStream()
2.使用输出流,将数据输出到客户端浏览器
案例
完成重定向
//设置状态码为302
response.setStatus(302);
//设置响应头location
response.setHeader("location","/Servlet02/ResponseDemo02");
System.out.println("demo01");
//简单重定向
response.sendRedirect("/Servlet02/ResponseDem002");
重定向特点:
1.地址栏发生变化
2.重定向可以访问其他站点(服务器)的资源
3.重定向是两次请求,不能使用request域对象共享数据
路径写法
1.路径分类
1.相对路径
:通过相对路径不可以确定唯一资源
./index.html 不以/开头 以.开头路径
规则:找到当前资源和目标资源之间的相对位置关系
./
当前目录
../
后退一级目录
2.绝对路径
:通过绝对路径可以确定唯一资源
以/
开头的目录
规则:判断定义的路径给谁用
给客户端浏览器使用,需要通过加虚拟目录(项目的访问路径)
建议虚拟目录动态获取,request.getContextPath()
;< a >< form >重定向
给服务器使用:不需要加虚拟目录
转发路径
服务器输出字符数据到浏览器
步骤
- 获取字符输出流
- 输出数据
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取流对象之前,设置流的默认编码,GBK(可以不写)
response.setCharacterEncoding("GBK");
//告诉浏览器,服务器发送的消息体数据的编码.建议浏览器使用的编码解码
response.setHeader("content-type","text/html;charset=utf-8");
//简洁写法(获取流之前设置)
//response.setContentType("text/html;charset=utf-8")
//获取字符输出流(默认编码:ISO-8859-1)
PrintWriter pw= response.getWriter();
//输出数据
pw.write("hello");
}
服务器输出字节数据到浏览器
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//获取字节输出流
ServletOutputStream sos = response.getOutputStream();
//输出数据
sos.write("hello".getBytes());
}
验证码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.onload=function ( ) {
var img = document.getElementById("checkcode");
img.onclick=function ( ) {
var date = new Date().getTime();
img.src="/Servlet02/Checkcode?"+date;
}
}
</script>
</head>
<body>
<img id="checkcode" src="/Servlet02/Checkcode"/>
<a id="change" href="">看不清楚换一张</a>
</body>
</html>
@WebServlet( "/Checkcode")
public class Checkcode extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width=100;
int height=50;
//创建一对象,在内存中画图(验证码图像对象)
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//美化图片
Graphics g = image.getGraphics();
g.setColor(Color.pink);
g.fillRect(0,0,width-1,height-1);
g.setColor(Color.black);
String string="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
for (int i = 1; i <=4 ; i++) {
int index = random.nextInt(string.length());
char ch = string.charAt(index);
g.drawString(ch+"",width/5*i,height/2);
}
g.setColor(Color.green);
for (int i = 0; i < 10; i++) {
int x1 = random.nextInt(width);
int x2 = random.nextInt(width);
int y1 = random.nextInt(height);
int y2 = random.nextInt(height);
g.drawLine(x1,x2,y1,y2);
}
//将图片输出到页面展示
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}