• Request请求转发
○ 请求转发§ 请求转发(forward):一种在服务器内部的资源跳转方式§ ○ 实现方式:§ request.getRequestDispatcher("资源B路径").forward(request,response);○ 请求转发资源间共享数据:使用Request对象§ void setAttribute(String name,Object object)存储数据到request域中§ Object getAttribute(String name)根据key,获取值§ void removeAttribute(String name)根据key,删除键值对○ 请求转发的特点:§ 浏览器地址栏路径不发生变化§ 只能转发到当前服务器的内部资源§ 一次请求,可以再转发的资源间使用request共享数据
@WebServlet("/req5")
public class RequestDemo5 extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("demo5....");//存储数据void setAttribute(String name, Object object)request.setAttribute("msg", "hello");//请求转发request.getRequestDispatcher("/req6").forward(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}@WebServlet("/req6")
public class RequestDemo6 extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("demo6...");//删除数据//request.removeAttribute("msg");//获取数据Object getAttribute(String name)Object msg = request.getAttribute("msg");System.out.println(msg);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
执行过程:
运行tomcat
点击链接
输入url 按回车
• Response
• Response设置响应数据功能介绍○ 响应数据分为3个部分:§ 响应行:HTTP/1.1 200 OK□ Voids etStatus(intsc)设置响应状态码§ 响应头:Content-Type:text/html□ Void setHeader(String name,String value):设置响应头键值对§ 响应体:<html><head></head><body></body></html>PrintWritergetWriter()获取字符输出流ServletOutputStreamgetOutputStream()获取字节输出流
• Response完成重定向○ 重定向(Redirect):一种资源跳转方式○ 实现方式§ void setStatus(int sc)□ response.setStatus(302);§ void setHeader(String name,String value)□ response.setHeader("location","/request-demo/resp2");○ 简化实现重定向的方式§ response.sendRedirect("/request-demo/resp2");○ 重定向的特点§ 浏览器地址栏路径发生改变§ 可以重定向到任意位置的资源(服务器内部、外部均可)§ 两次请求,不能在多个资源使用request共享数据○ 路径问题□ 浏览器使用:需要加虚拟目录(项目访问路径)□ 服务端使用:不需要加虚拟目录
@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("resp1...");//重定向//1.设置响应状态码 void setStatus(int sc)/*response.setStatus(302);//2.设置响应头 void setHeader(String name, String value)response.setHeader("location", "/request-demo/resp2");*///简化方式完成重定向//动态获取虚拟目录String contextPath = request.getContextPath();response.sendRedirect(contextPath + "/resp2");
// response.sendRedirect("https://www.itcast.cn");}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}@WebServlet("/resp2")
public class ResponseDemo2 extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("resp2...");}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
执行过程 :
启动tomcat,如果已经启动则重运行即可
点击链接
输入url,按回车