


http是无状态的,没有记忆能力,前一条数据和后一条数据不保存连接
请求协议:




Http响应协议:


3开头的状态码为重定向:
浏览器现象A服务器发送请求,A响应内容不在他这在B里面,返回Location为B让浏览器指向B

404表示客户端不存在未找到
5开头的状态码是服务端错误



java
package com.itheima;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class ResponseController {
/*
* 方式一
* */
@RequestMapping("/response")
public void response(HttpServletResponse response) throws IOException {
//1.设置响应状态码
response.setStatus(200);
//2.设置响应头
response.setHeader("name","chn");
//3.设置响应体
response.getWriter().write("<h1>hello world</h1>");
}
/*
* 方式二
* */
@RequestMapping("/response2")
public ResponseEntity<String> response2(){
return ResponseEntity.status(401).
header("name","chn").
body("<h1>hello world</h1>");
}
}
