forward和redirect的区别
1.ForWard的转发的实现过程:通俗的讲它相当于现实生活中的一次性用品,跳转到下一个页面之后就死了(没用了)
而且它不会显示本页的东西,只会显示你跳转到的第二个页面的内容,而且它发生在同一次请求中的。
测试代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2><span style="color:red">你好你是中国人么?</span></h2>
<%request.setAttribute("name", "Mike"); %>
<%request.setAttribute("password", "pwd"); %>
<%request.getRequestDispatcher("page2.jsp").forward(request, response); %>
<%out.write("你是谁?"); %>
</body>
</html>转发到的页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name = (String)request.getAttribute("name");
String pwd = (String)request.getAttribute("password");
out.println("Name="+name);
out.println("pwd="+pwd);
out.println("你好同学们你现在懂了吗?");
%>
<h2><span style="color:red">你好你是中国人么?</span></h2>
</body>
</html>2.重定向:什么叫从定向呢?
官方式讲法就是至少要通过两次请求
通俗的讲法是它会跳转到第二个页面的地址,显示第二个页面的内容。
代码如上把<%request.getRequestDispatcher("page2.jsp").forward(request, response); %>
改成如下:
response.sendRedirect("page2.jsp");测试下就知道了
总结forward和redirect的区别就是:
forward:是发生在同一次请求中的。
redirect:是发生在两次请求中的。
相关推荐
lupeng 2020-11-14
sjcheck 2020-11-10
meylovezn 2020-08-28
owhile 2020-08-18
Francismingren 2020-08-17
pythonclass 2020-07-29
sunzhihaofuture 2020-07-19
爱读书的旅行者 2020-07-07
行吟阁 2020-07-05
tianqi 2020-07-05
行吟阁 2020-07-04
冰蝶 2020-07-04
lyg0 2020-07-04
owhile 2020-07-04
opspider 2020-06-28
lengyu0 2020-06-28
tianqi 2020-06-21
dadaooxx 2020-06-16