Request请求转发
服务器内部资源跳转方式
示例代码
假设我们在执行req4请求的时候要跳转到req5上
@WebServlet("/req4") public class RequestDemo4 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("demo4..."); //请求代码 req.getRequestDispatcher("/req5").forward(req,resp); } }
@WebServlet("/req5") public class RequestDemo5 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("demo5..."); } }
运行结果:
请求转发间共享数据
请求转发特点:
1.浏览器地址栏路径不发生变化
2.只能转发到当前服务器的内部资源
3.一次请求,可以在转发的资源间使用request共享数据
Response
Response设置响应数据功能&完成重定向
响应数据分为3部分:
1.响应行
2.响应头
3.响应体
什么是重定向?
一种资源跳转方式
示例代码:
@WebServlet("/resp1") public class ResponseDemo1 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("resp1....."); //重定向 //1.设置响应状态码 resp.setStatus(302); //2.设置响应头 location resp.setHeader("Location","/resp2"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); } }
@WebServlet("/resp2") public class ResponseDemo2 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("resp2....."); } }
重定向简化方法:
重定向特点:
1.浏览器地址栏路径发生变化
2.可以重定向到任意位置的资源(服务器内部,外部均可)
3.两次请求,不能在多个资源中使用request共享数据
路径问题
什么时候路径需要加虚拟目录?
首先你得明确这个路径给谁使用,如果是给浏览器使用,需要加虚拟目录,如果是给服务器使用,不需要加虚拟目录。
例如:
Response 响应字符数据
示例代码:
@WebServlet("/resp3") public class ResponseDemo3 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取字符输出流 PrintWriter writer = resp.getWriter(); //content-type resp.setHeader("content-type","text/html"); writer.write("aaa"); writer.write("<h1>aaa</h1>"); } }
运行结果:
1.writer流不要关闭,因为这个流是随着response对象获取出来的,等这次请求响应完毕后,response对象会被销毁,销毁的时候,writer也会被关闭
2.
这样的话就可以响应中文了
Response 响应字节数据
示例代码:
@WebServlet("/resp4") public class ResponseDemo4 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.读取文件 FileInputStream fis = new FileInputStream("C://Users//Administrator//Desktop//1.jpg"); //2.获取response字节输出流 ServletOutputStream os = resp.getOutputStream(); //完成流的copy byte[] buff = new byte[1024]; int len = 0; while((len=fis.read(buff))!=-1){ os.write(buff,0,len); } fis.close(); } }