一、HttpServletResponse概述
我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。
service方法中的response的类型是ServletResponse,而doGet/doPost方法的response的类型是HttpServletResponse,HttpServletResponse是ServletResponse的子接口,功能和方法更加强大
二、response的运行流程
三、通过抓包工具抓取Http响应
因为response代表响应,所以我们可以通过该对象分别设置Http响应的响应行,响应头和响应体
四、通过response设置响应行中的状态码
设置响应行的状态码
setStatus(int sc)
setStatus(int sc)
package com.itheima.line;
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 StatusServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//手动设置http响应行中的状态码
response.setStatus(404);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
五、通过response设置响应头
addHeader(String name, String value)addIntHeader(String name, int value)
addDateHeader(String name, long date)
setHeader(String name, String value)
setDateHeader(String name, long date)
setIntHeader(String name, int value)
其中,add表示添加,而set表示设置
package com.itheima.header;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HeaderServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Date date = new Date();
//设置响应头
response.addHeader("name", "zhangsan");
//response.addIntHeader("age", 28);
//response.addDateHeader("birthday", date.getTime());
response.addHeader("name", "lisi");
response.setHeader("age", "28");
response.setHeader("age", "50");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
重定向:
package com.itheima.header;
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 Servlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//没有响应 告知客户端去重定向到servlet2
//1、设置状态码302
//response.setStatus(302);
//2、设置响应头Location
//response.setHeader("Location", "/WEB14/servlet2");
//封装成一个重定向的方法sendRedirect(url)
response.sendRedirect("/WEB14/servlet2");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.itheima.header;
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 Servlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("hello haohao...");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
设置定时刷新的头:
package com.itheima.header;
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 {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置定时刷新的头
response.setHeader("refresh", "5;url=http://www.baidu.com");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
六、通过response设置响应体
(1)响应体设置文本PrintWriter getWriter()
获得字符流,通过字符流的write(String s)方法可以将字符串设置到response 缓冲区中,随后Tomcat会将response缓冲区中的内容组装成Http响应返回给浏览 器端。
关于设置中文的乱码问题
原因:response缓冲区的默认编码是iso8859-1,此码表中没有中文,可以通过 response的setCharacterEncoding(String charset) 设置response的编码
但我们发现客户端还是不能正常显示文字
原因:我们将response缓冲区的编码设置成UTF-8,但浏览器的默认编码是本地系统的编码,因为我们都是中文系统,所以客户端浏览器的默认编码是GBK,我们可以手动修改浏览器的编码是UTF-8。
我们还可以在代码中指定浏览器解析页面的编码方式,
通过response的setContentType(String type)方法指定页面解析时的编码是UTF-8
response.setContentType("text/html;charset=UTF-8");
上面的代码不仅可以指定浏览器解析页面时的编码,同时也内含setCharacterEncoding的功能,所以在实际开发中只要编写response.setContentType("text/html;charset=UTF-8");就可以解决页面输出中文乱码问题。
(2)响应头设置字节
ServletOutputStream getOutputStream()
获得字节流,通过该字节流的write(byte[] bytes)可以向response缓冲区中写入字节,在由Tomcat服务器将字节内容组成Http响应返回给浏览器。
七、案例-完成文件下载
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>使用a标签直接指向服务器上的资源</h1>
<a href="/web14_response/download/a.flv">a.flv</a><br>
<a href="/web14_response/download/a.jpg">a.jpg</a><br>
<a href="/web14_response/download/a.mp3">a.mp3</a><br>
<a href="/web14_response/download/a.mp4">a.mp4</a><br>
<a href="/web14_response/download/a.txt">a.txt</a><br>
<a href="/web14_response/download/a.zip">a.zip</a><br>
<h1>使用服务端编码的方式实现文件下载</h1>
<a href="/web14_response/DownloadServlet?filename=a.flv">a.flv</a><br>
<a href="/web14_response/DownloadServlet?filename=a.jpg">a.jpg</a><br>
<a href="/web14_response/DownloadServlet?filename=a.mp3">a.mp3</a><br>
<a href="/web14_response/DownloadServlet?filename=a.mp4">a.mp4</a><br>
<a href="/web14_response/DownloadServlet?filename=a.txt">a.txt</a><br>
<a href="/web14_response/DownloadServlet?filename=a.zip">a.zip</a><br>
</body>
</html>
package com.sh.content;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得要下载文件按的名称
String filename = request.getParameter("filename");
//要下载这个文件的类型---------客户端通过文件的MIME类型去区分文件类型
response.setContentType(this.getServletContext().getMimeType(filename));
//告诉客户端该文件不是直接解析,而是以附件的形式打开(打开)
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//获得文件的绝对路径
String path = this.getServletContext().getRealPath("download/"+filename);
//获得该文件的输入流
InputStream is = new FileInputStream(path);
//获得输出流----通过response获得输出流 用于向客户端写内容
ServletOutputStream sos = response.getOutputStream();
//文件拷贝的模板
int len = 0;
byte[] buf = new byte[1024];
while((len=is.read(buf))!=-1) {
sos.write(buf, 0, len);
}
is.close();
sos.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}