0309 HttpServletResponse01

首先回顾一下Servlet运行流程

 

 

 服务器将响应封装到了response对象中返回给客户端

那么response对象中就封装了 响应行,响应头和响应体

1、通过response设置响应行

响应hang中包括协议版本号,状态码和状态信息

setStatus(int sc)方法可以设置状态码

代码展示 让当访问servlet的时候报404状态

public class LineServlet 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);
	}
}

  

2、通过response设置响应头

常用头

Location:指定响应路径,与状态码302配合使用完成跳转

Refresh:定时刷新,格式:秒数;url=路径。url可省略,默认值为当前页 例:3;url=www.baidu.com 三秒后跳转到百度网

Content-Type:响应正文的类型 值:text/html;charset=UTF-8

设置响应头方法

主要:setHeader(String name, String value)  name是头的名字,value 是值

代码展示:servlet01设置跳转到servlet02

(1)使用location 搭配302状态码跳转

public class Servlet01 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//完成重定向
		/*response.setStatus(302);
		response.setHeader("Location", "/WEB03/Servlet02");*/
		//方法调用
		response.sendRedirect("/WEB03/Servlet02");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

  注:上述代码中sendRedirect()方法中封装了上两行代码,直接调用可以使用

(2)使用Refresh定时刷新 五秒跳转

public class HeadServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().write("hello dandan...");
		//定时刷新
		response.setHeader("Refresh", "5;url=https://www.baidu.com");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

  (3)用前端js写 jsp文件

<%@ 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>
</head>
<body>
	恭喜您注册成功!
	<span id="second">5</span>
	秒后跳转,如不跳转,
	请<a href="https://www.baidu.com">点击这里</a>
</body>
<script type="text/javascript" >
	//获取元素
	var second=document.getElementById("second");
	var timer=5;
	//设置定时器
	var time=setInterval(function(){
		second.innerHTML=timer;
		timer--;
		if(timer<0){
			//取消定时器
			clearInterval(time);
			//跳转
			location.href="https://www.baidu.com";
		}
	}, 1000);
</script>
</html>

  (4)用前端jquery写 html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
恭喜您注册成功   <span id="s">5</span> 秒后跳转 ,如不跳转,
<a href="https://www.baidu.com">请点击这里</a>
</body>
<script type="text/javascript" src="/WEB03/js/jquery-3.4.0.min.js"></script>
<script type="text/javascript">
var time=5;
var timer=setInterval(function(){
	$("#s").html(time);
	time--;
	if(time<0){
		clearInterval(timer);
		location.href="https://www.baidu.com";
	}
}, 1000);
</script>
</html>

  

3、response设置响应体

(1)响应体设置文本

PrintWriter getWriter() 方法获取字符流 通过write方法将数据写入到response缓冲区中,tomcat检测到write方法结束以后就会到response缓冲区中去读取内容 并封装成响应行响应头和响应体返回给客户端浏览器

乱码问题:

response缓冲区码表是iOS8859-1码表 客户端浏览器是jbk码表,所以我们要用setCharacterEncoding(String charset)方法去设置response缓冲区的码表,用setContentType(String type)方法指定客户端浏览器码表(其中setContentType(String type)方法封装了setCharacterEncoding(String charset)方法 所以直接用setContentType(String type)方法就可以实现

代码展示

public class bodyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置码表 缓冲区中的码表
		response.setCharacterEncoding("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);
	}
}

  

 

 

 (2)字节

ServletOutputStream  getOutputStream()获取字节输出流

我们可以通过字节输入流读取一个图片 通过字节输出流响应给客户端浏览器

代码展示

public class picServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path=getServletContext().getRealPath("download/3.jpg"); 
		//明确数据源
		FileInputStream fis=new FileInputStream(path);
		//明确目的地
		ServletOutputStream out=response.getOutputStream();
		byte[] bytes=new byte[1024];
		int len=0;
		while((len=fis.read(bytes))!=-1){
			out.write(bytes,0,len);
		}
		fis.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

 

 那我们输出的字节 能解析的就给解析了,直接显示一张图片 那我们如果指定一个压缩包,他就会解析不了就会提供下载功能

如果把上述代码改成3.rar

 

 就会提示下载文件

 

posted @ 2021-03-09 11:50  公雪  阅读(116)  评论(0编辑  收藏  举报