文章目录
- response概述
- (1)什么是reponse?‘
- (2)reponse对象
- response-设置响应行
- 设置响应头
- response-重定向**
- (1)什么叫重定向?
- (2)重定向的核心
- 方法1
- 方法二
- response实现自动刷新跳转
- response-设置响应体
- getWriter()方法
- getOutputstream()方法
- 浏览器访问Servlet显示图片
- 超链接访问文件
- Response中应注意的细节
response概述
(1)什么是reponse?‘
response是服务器对服务器响应的封装,HTTP响应消息分为状态行、响应消息头、消息体三部分
(2)reponse对象
HttpServletResponse对象,通过它的方法可以设置HTTP响应消息的内容
在Servlet API中,定义了一个HttpServletResponse接口,它继承自ServletResponse接口,专门用来封装HTTP响应消息。由于,因此,在HttpServletResponse接口中定义了向客户端发送响应状态码、响应消息头、响应消息体的方法
以setXxx()方法为主
response-设置响应行
HTTP/1.1 200 OK
- (1)常见状态码的含义
在这里插入图片描述 - (2)设置响应行
设置状态码: 200 302 304 404 500
setStatus(int status)方法 200 302 304
sendError(int sc)方法 404 500
src\com\wzx\pack01_setline\Demo01SetLineServlet.java
@WebServlet("/set_line")
public class Demo01SetLineServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet...");
//1:设置响应行里面的状态码
//response.setStatus(500);
//response.sendError(500); 有对应的错误提示页面
}
}
设置响应头
- (1)响应头是什么?
响应头是一组键值对 - (2)设置响应头有什么用?
1)设置自己的键值对 name:jack
2)修改系统已经存在的键值对 - (3)设置方法
void setHeader(String name, String value)
//设置响应头的名字:Content-Type的值
void setHeader("Content-Type", String value)
void setContentType(String type)
//设置响应头,5秒钟之后,页面自动跳转到/day14login/index.html
response.setHeader("Refresh", "5;url=/day14login/index.html");
setHeader("Content-Disposition", String value)
src\com\wzx\pack02_sethead\Demo02SetHeadServlet.java
@WebServlet("/set_head")
public class Demo02SetHeadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:在响应头里面添加自己定义的key-value
response.setHeader("name","jack");
response.setHeader("gf","rose");
//2:修改key-value的值
//Content-Type: text/html;charset=utf-8
//response.setHeader("Content-Type","text/html;charset=utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("中国");
}
}
response-重定向**
(1)什么叫重定向?
两次请求,两次响应
浏览器访问一个Servlet1,Servlet1没有我想要的资源,Servlet1让浏览器找Servlet2
(2)重定向的核心
响应码302
响应头Location
方法1
response.setStatus(302); //设置响应行的状态码为302 重定向
response.setHeader("Location", "/Servlet/servlet2"); //设置响应头的属性 跳转到Servlet2
方法二
response.sendRedirect("/Servlet/servlet2"); //利用response中的sendRedirect属性完成重定向
response实现自动刷新跳转
public class RefreshServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("refresh", "5;url=http://www.baidu.com"); //设置头 参数 数值5为
//秒 中间用分号间隔 url为要跳转的网址
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
使用JS来完成自动跳转
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
window.onload= function(){
var time = 3;
var second = document.getElementById("second");
timer = setInterval(function(){
second.innerHTML = time;
time--;
if(time==0){
location.href = "http://www.baidu.com";
clearInterval(timer);
}
},1000);
}
</script>
</head>
<body>
恭喜你,登录成功,<span id="second" style="color: red">3</span>秒后将跳转,若不跳转 请点击<a href="http://www.baidu.com">这里</a>
</body>
</html>
response-设置响应体
getWriter()方法
- (1)设置响应体的方法
- 1)getWriter()方法:字符流
只能向浏览器响应文本内容: 字符串,标签
为什么会产生中文乱码?Tomcat IOS 8859-1 - 2)getOutputStream()方法:字节流
可以向浏览器响应任何类型的数据: 图片,视频,音频
用于开发下载功能
src\com\wzx\pack04_body\Demo04Servlet.java
@WebServlet("/body")
public class Demo04Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:字符流
response.setHeader("Content-Type","text/html;charset=utf-8");
response.getWriter().write("<font color='red'>中国</font>");
}
}
getOutputstream()方法
(1)字节输出流
a:如果向页面输出字符串,不需要解决乱码
b: 向页面输出字符串时,使用write
src\com\wzx\pack04_body\Demo05Servlet.java
写字符串一般使用字符流,但是必须要处理编码。而使用字节流能搞定字符串?能~且不需要处理编码,但是字节流真正的作用是写文件数据
@WebServlet("/body2")
public class Demo05Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取字节输出流
ServletOutputStream outputStream = response.getOutputStream();
//写数据到浏览器
byte[] bytes = "中国".getBytes();
outputStream.write(bytes);
//关闭
outputStream.close();
}
}
src\com\wzx\service\FileService.java
//文件业务类
public class FileService {
//复制文件
public void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
//边读边写
//1:字节数组 缓冲区
byte[] buffer=new byte[1024];
int len = 0;
//2:边读边写
while ((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
}
}
浏览器访问Servlet显示图片
本质 就是 将一个文件的所有字节,复制到浏览器
src\com\wzx\pack05_download\Demo05Servlet.java
@WebServlet("/download")
public class Demo05Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.加载a.jpg文件成为一个输入流
InputStream inputStream = getServletContext().getResourceAsStream("download/a.mp3");
//2.再使用字节流 将数据写到浏览器
OutputStream outputStream = response.getOutputStream();
//3.浏览器自动将数据显示成图片
FileService fileService = new FileService();
fileService.copy(inputStream,outputStream);
//4.关闭
outputStream.close();
inputStream.close();
}
}
超链接访问文件
(1)超链接访问图片
浏览器本身支持,本质也是先读文件,再将文件写到浏览器
web\index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/web01_reponse/download/a.jpg">点一点看图片</a><br/>
<a href="/web01_reponse/download/a.mp3">点一点看mp3</a><br/>
<a href="/web01_reponse/download/a.mp4">点一点看mp4</a><br/>
<a href="/web01_reponse/download/a.zip">点一点看zip</a><br/>
</body>
</html>
Response中应注意的细节
1.response获得的流OutputStream不需要手动关闭,Tomcat容器会帮助我们关闭 但是InputStream需要手动关闭
2.getWriter和getOutputStream不能同时调用