Response
设置相应数据
响应数据分为三部分
1.响应行 HTTP/1.1 200 OK
void setStatus(int sc):设置响应状态码
2.响应头 Content-Type:text/html
void setHeader(String name,String value):设置响应头键值对
3.响应体: <html><head></head><body></body></html>
PrintWriter getWriter():获取字符输出流
ServletOutputStream getOutputStream();获取字节输出流
Respone重定向
一种资源跳转的方式
代码演示:访问resp1跳转到resp2
@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("resp1被访问");
//设置相应码
resp.setStatus(302);
//设置响应头location
resp.setHeader("location","/response/resp2");
//简化
resp.sendRedirect("www.baidu.com");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
@WebServlet("/resp2")
public class ResponseDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("resp2被访问");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
请求转发特点 :
浏览器地址不会发生变化
只能转发服务器内部资源
一次请求
重定向特点:
浏览器地址栏路径发生变化
可以重定向到任意位置的资源(服务器内部,外部均可)
两次请求
当重定向和转发都可以的时候 选择转发 因为转发速度快
重定向应用场景
域名配置:比如输入baidu.com和www.baidu.com都可以访问
https协议迁移:比如输入http://www.baidu.com和https://www.baidu.com都可以访问百度
网站迁移:比如一个网站更换域名后,还保留之前域名
URL路径问题
大家可以看到 转发直接是/req* 重定向则加了一个包名 接下来就给大家解释一下
浏览器使用:需要加虚拟目录(项目访问路径)
服务端使用:不需要加虚拟目录
例如:超链接 表单 给浏览器用的 要加虚拟目录
req.getRequestDispatcher("路径"):转发不加
resq.sendRediect("路径");重定向 加 (浏览器用)
Response响应字符数据
Response对象设置响应体
PrintWriter getWriter();获取字符输出流
ServletOutputStream getOutputStream();获取字节输出流
使用:
1.通过Response对象获取字符输出流
ServletOutputStream os = resp.getOutputStream();
2.写数据
os.write(字节数据);
注意:该流不需要关闭,随着响应结束,response对象销毁,由服务器关闭
中文数据乱码,原因通过Response获取的字符输出流默认编码:ISO-8859-1
resp.setContentType("text/html;charset=utf-8");
/*
响应字符数据
*/
@WebServlet("/resp3")
public class ResponseDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
//获取字符输出流
PrintWriter writer = resp.getWriter();
//2.写数据
writer.write("style");
writer.write("保持学习");
//流不用关闭 tomcat 帮忙关闭
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
Response响应字节数据
Response对象设置响应体
PrintWriter getWriter();获取字符输出流
ServletOutputStream getOutputStream();获取字节输出流
使用:
1.通过Response对象获取字符输出流
PrintWriter writer=resp.getWriter();
2.写数据
writer.writer("aaa");
注意:该流不需要关闭,随着响应结束,response对象销毁,由服务器关闭
/*
响应字节数据
将图片写入客户端
*/
@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;utf-8");
// ServletOutputStream sos = resp.getOutputStream();
//
// sos.write("jiayouiayou".getBytes(StandardCharsets.UTF_8));
//创建字节输入流
FileInputStream fis = new FileInputStream("C:\\Users\\帅星\\Desktop\\111.jpg");
//获取字节输出流
ServletOutputStream os = resp.getOutputStream();
//流的对拷
// byte[] buff=new byte[1024];
// int len=0;
// while ((len=fis.read(buff))!=0){
// os.write(buff,0,len);
// }
//简写
//前提pom文件导入IO 坐标
// <dependency>
// <groupId>org.apache.commons</groupId>
// <artifactId>commons-io</artifactId>
// <version>1.3.2</version>
// </dependency>
IOUtils.copy(fis, os);
fis.close();
os.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}