首页 > 编程语言 > Java HttpServletResponse响应实现过程详解
2020
09-29

Java HttpServletResponse响应实现过程详解

 用户在客户端输入网址(虚拟路径)时,开始发送一个HTTP请求(请求行、请求头、请求体)至服务器。服务器内的Tomcat引擎会解析请求的地址,去找XML文件,然后根据虚拟路径找Servlet的真实路径,真实的Servlet会将请求的信息封装成request(请求)对象,然后再创建一个response(响应)对象,(此时的response内是空的)同时创建servlet对象,并调用service方法(或doGet和doPost方法)。

这样就是把两个对象传给了服务器内的某个servlet的service方法,通过这个方法,我们可以获得request的所有的信息,并且向response内设置信息。response.getwriter().write()将内容写到response的缓冲区,这样service方法结束了,方法返回后,tomcat引擎会将从该response缓冲区中获取的设置信息封装成一个HTTP响应(响应行、响应头、响应体),发送给客户端。客户端解析响应回来的东西继而进行显示。

概述:

我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。service方法中的response的类型是ServletResponse,而doGet/doPost方法的response的类型是HttpServletResponse,HttpServletResponse是ServletResponse的子接口,功能和方法更加强大

通过response 设置响应行:

  设置响应行的状态码:setStatus( int sc)

通过response 设置响应头:

  setHeader(String name,String value) 设置

三秒以后跳转到百度:

public class RefreshServlet extends HttpServlet {

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //设置定时刷新的头
    response.setHeader("refresh","5;url=https://www.baidu.com");
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
  window.onload=function(){
    //获取span元素
    var second=document.getElementById("second");
    //定义秒数
    var time =5;
    //设置定时器
    var timer=setInterval(function(){
      second.innerHTML=time;
      time--;
      if(time < 0){
        clearInterval(timer);
        location.href="https://www.baidu.com" rel="external nofollow" rel="external nofollow" ;
      }
    },1000);
  }
  
</script>
</head>
<body>
  恭喜您,注册成功!
  <span id="second" style="color:red">5</span>
  秒后跳转,如没跳转,请点击<a href="https://www.baidu.com" rel="external nofollow" rel="external nofollow" >这里</a>
</body>
</html>

重定向:(请求服务器两次,地址栏变化)

①、状态码:302;

②、响应头:location 代表重定向地址;

public class Servlet01 extends HttpServlet {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    /*// 设置响应状态码
    response.setStatus(302);
    //设置响应头中的Location
    response.setHeader("Location","/WEB0/Servlet02");*/
    //重定向
    response.sendRedirect("/WEB0/Servlet02");
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }

}
public class Servlet02 extends HttpServlet {
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().write("Servlet02");
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧