目录
HTTP响应报文协议
reponse继承体系
reponse的方法
响应行
public void setStatus(int sc)
响应头
public void setHeader(String name, String value)
响应体
public java.io.PrintWriter getWriter()
public ServletOutputStream getOutputStream()
请求重定向
请求转发
public RequestDispatchergetRequestDispatcher(String path)
void forward(ServletRequest var1, ServletResponse var2)
HTTP响应报文协议
前端页面
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h2>GET请求</h2><form action="/reGetServlet" method="get"><input type="text" name="username"/> <br><input type="text" name="password"/> <br><input type="submit" value="提交"> <br></form><h2>POST请求</h2><form action="/postServlet" method="post"><input type="text" name="username"/> <br><input type="text" name="password"/> <br><input type="submit" value="提交"> <br></form> </body> </html>
servlet
import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException;@WebServlet("/reGetServlet") public class ReGetServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.getWriter().println("response...");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }
浏览器
响应行
响应头
响应体
总结
响应报文协议:服务器发给浏览器的数据遵守的规范
响应报文协议包括:
1.响应行
2.响应头
3.响应体
响应行:
1.响应状态码 200 404 500 405 302 3042.响应协议/版本 http/1.1
响应头:
服务器时间响应数据的字符个数
响应体:
服务器响应给浏览器的数据如果浏览器请求的是静态资源页面,那么tomcat服务器就会响应给浏览器静态资源
reponse继承体系
因为ServletReponse和HttpServletReponse都是接口,不能创建出对象,所以tomcat会在底层创建ReponseFacade门面类对象-->ServletReponse reponse=new ReponseFacade();
然后将响应对象传递给service的doGet() doPost()方法。
reponse的方法
响应行
public void setStatus(int sc)
设置此响应的状态代码。此方法用于设置没有错误时的返回状态代码
响应头
public void setHeader(String name, String value)
用给定名称和值设置响应头。如果已经设置了头,则新值将重写以前的值。
containsHeader
方法可用于测试在设置其值之前头是否存在。
name 头的名称 value 头值,如果该值包含八位字节字符串,则应该根据 RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) 对其进行编码 See also containsHeader, addHeader
响应体
对于响应体,是通过字符,字节输出流的方式往浏览器写
public java.io.PrintWriter getWriter()
获取字符输出流
返回可将字符文本发送到客户端的
PrintWriter
对象。
public ServletOutputStream getOutputStream()
获取字节输出流
此方法的默认行为是返回对包装的响应对象调用 getOutputStream() 的结果。
请求重定向
重定向:属于服务器外部跳转的行为。当服务器某个资源没有处理业务的能力,会将新的服务器地址告知浏览器(location),并设置状态码304。浏览器会拿着新的地址再次向服务器发送请求
response.setStatus(302);
response.setHeader("location","资源B的访问路径")
特点
1. 服务器外部跳转2.两次请求和响应
3.地址栏发生改变,记录最后一次的地址
4.不能共享request域对象
测试
@WebServlet("/reponseDemo1") public class reponseDemo1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("我无法处理");/* response.setStatus(302);//设置状态码response.setHeader("location","/reponseDemo2");//告知资源B的地址*///简写response.sendRedirect("/reponseDemo2");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);B} }
@WebServlet("/reponseDemo2") public class reponseDemo2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("我可以处理");//响应数据给浏览器response.getWriter().println("success...");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }
浏览器:最后的地址的responseDemo2
请求转发
使用
public RequestDispatchergetRequestDispatcher(String path)
path:要跳转服务器的路径
void forward(ServletRequest var1, ServletResponse var2)
特点
1. 服务器内部跳转2.一次请求和响应
3.地址栏没有改变
4.共享request域对象
测试
@WebServlet("/responseDemo3") public class responseDemo3 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//请求转发System.out.println("responseDemo3...");//向request域对象存储数据request.setAttribute("msg","我无法解析");//获取转发器RequestDispatcher requestDispatcher = request.getRequestDispatcher("/responseDemo4");requestDispatcher.forward(request,response);//转发}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }
@WebServlet("/responseDemo4") public class responseDemo4 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("responseDemo4...");//获取request域对象的数据Object msg = request.getAttribute("msg");System.out.println(msg);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }
浏览器页面:地址没有变