转发(服务端行为)
形式:
request.getRequestDispatcher(url).forward(request,response) 转发在服务器端发挥作用,通过forward()方法提交信息在多个页面之间进行传递。
- 地址栏不会改变
- 转发只能转发到当前Web应用内的资源
- 在转发过程中,可以将数据保存到request域对象中去
- 转发只有一次请求 转发是服务器端行为
index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
结果:${msg}
</body>
</html>
写一个转发的控制器
@RequestMapping("/zhuanfa")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException, ServletException {
req.setAttribute("msg","这是转发");
req.getRequestDispatcher("index.jsp").forward(req,rsp);
}
重定向(客户端行为)
形式:response.sendRedirect("");
- 重定向地址栏会改变
- 重定向可以跳转到当前web应用,甚至是外部域名网站
- 不能在重定向的过程中,将数据保存到request域对象中。
写一个重定向控制器
@RequestMapping("/cdingxiang")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException, ServletException {
req.setAttribute("msg","这是重定向");
rsp.sendRedirect("index.jsp");
}