• HttpServletResponse概述


    response的运行流程

    response:

              1. 属于重定向请求;

              2.  其地址栏的URL会改变;

               3.会向服务器发送两次请求;

    用response,A没有钱,请求失败,但是A告诉B,C有钱。于是B再次向C借钱,C借给B,请求成功。

    response是响应对象。

    通过response设置响应行

    设置响应行的状态码

    setStatus(int sc)

    package com.oracle.demo01;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class HeadServlet extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//设置响应行中的状态码(只能改变响应行中的状态码,版本号与状态信息无法改变,状态信息随着状态的改变而改变)
    		response.setStatus(404);
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }

     

    通过response设置响应头

    有众多方法,但是常用的就是setHeader(String name,String value)

    重定向需要:1.状态码:302

          2.响应头:location 代表重定向地址

    使用该方法设置的响应头最终会发送给客户端浏览器

    package com.oracle.demo01;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.sun.org.apache.regexp.internal.RESyntaxException;
    
    public class Servlet01 extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    /*		//设置响应行状态码
    		response.setStatus(302);
    	      //设置响应头中的Location
    		response.setHeader("Location","/WEB05/Servlet02");*/
    		//重定向(与上面的代码是一致的)简洁版
    		response.sendRedirect("/WEB05/Servlet02");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }

    设置定时刷新响应头的信息

    package com.oracle.demo01;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class RefreshServlet extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//设置定时刷新的头
    		response.setHeader("Refresh","5;url=https://www.baidu.com");
    	}
    
    	public 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";
    		 }
    	 },1000);
     }
    </script>
    </head>
    <body>
      	 恭喜你,注册成功!
      	 <span id="second" style="color:red">5</span>
      	 秒后跳转,如没跳转,请点击
      	 <a href="https://www.baidu.com">这里</a>
    </body>
    </html>
    

      

    通过response设置响应体

    解决中文乱码问题

    需要设置两个地方的中文乱码问题,一个是缓冲区的时候设置,一个是客户端解码的时候进行设置。

    package com.oracle.demo01;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class BodyServlet extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		/*//设置缓冲区的编码表
    		response.setCharacterEncoding("UTF-8");
    		//设置客户端浏览器
    		response.setHeader("Content-Type","text/html;charset=utf-8");*/
    		//解决响应的中文乱码问题(与上面的代码是一样的,简洁版)
    		response.setContentType("text/html;charset=utf-8");
    		response.getWriter().write("中国");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    

      

  • 相关阅读:
    ADO.NET 之 二 yz
    ADO.NET 之 三 yz
    telerik的RadAutoCompleteBox控件学习完结 yz
    ADO.NET 之 三 2 yz
    ASP.NET 底层技术 一 yz
    telerik的RadAutoCompleteBox控件学习三 yz
    ASP.NET 之 自定义 异步HTTP处理程序 yz
    javascriptArray类型 二 yz
    telerik的RadAutoCompleteBox控件学习一 yz
    javascript——Array类型 yz
  • 原文地址:https://www.cnblogs.com/awdsjk/p/11120261.html
Copyright © 2020-2023  润新知