HTTP协议
- 请求消息:客户端发送给服务器的数据
- 响应消息:服务器端发送给客户端的数据
- 数据格式:
- 响应行
- 组成:协议/版本 相应状态码 状态码描述
- 相应状态码:服务器告诉客户端浏览器本次请求和相应的一个状态
- 状态码都是3为数字
- 分类:
- 1**:服务器接收客户端消息,但没有接收完成,等待一段时间后,发送1**多状态码
- 2**:成功,例:200
- 3**:重定向,例:302(重定向),304(访问缓存)
- 4**:客户端错误,例:404(请求路径没有对应的资源),405(请求方式没有对应的方法)
- 5**:服务器端错误,例: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被访问");
response.sendRedirect("/duing2/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);
}
}
- 访问demo1资源后:
- 重定向与转发的区别:
- 重定向的特点
- 地址栏可以发生变化
- 重定向可以访问其他站点(服务器的)资源
- 重定向值是两次请求,不能使用request来共享数据
- 转发的特点:
- 转发地址栏路径不变
- 转发只能访问当前服务器下的资源
- 转发是一次请求,可以使用request对象来共享数据
- 路径写法
- 路径的分类
- 相对路径:通过相对路径不可以确定唯一资源
- 如:./index.html
- 不易/开头,以.开头
- 规则:确定访问的当前资源和目标资源之间的相对位置关系
- 绝对路径:通过绝对路径可以确定唯一资源
- 如:https://localhost/duing/responseDemo2
- 以/开头的路径
- 规则:判断定义的路径是给谁用的?判断请求从哪发出
- 给客户端浏览器使用:需要去加虚拟目录(项目的访问路径)
- 建议虚拟目录动态获取:
request.getContextPath()
- 给服务器使用:不需要加虚拟目录
案例:输出字符数据到浏览器
- 步骤:
- 获取字符输出流
- 输出数据
- 注意:乱码问题
- 原因:服务器发送的数据编码和浏览器解析的编码不一致
- 解决方法:
- 设置该流的编码
- 告诉浏览器响应体使用的编码
response.setContentType("text/html;charset=utf-8");
- 代码:
@WebServlet("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.write("<h1>你好 response</h1>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
案例:输出字节数据到浏览器
@WebServlet("/responseDemo5")
public class ResponseDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
ServletOutputStream sos = response.getOutputStream();
sos.write("欢迎".getBytes("utf-8"));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
- 访问