Jsp 的请求与重定向

来源:互联网 发布:淘宝详情页模板视频 编辑:程序博客网 时间:2024/05/17 05:12

forward_and_redirect_A.jsp:

<%@ 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>

<!-- 
4. 请求的转发和重定向:
1). 本质区别: 请求的转发只发出了一次请求, 而重定向则发出了两次请求. 
具体:


①. 请求的转发: 地址栏是初次发出请求的地址.
      请求的重定向: 地址栏不再是初次发出的请求地址. 地址栏为最后响应的那个地址 
       
②. 请求转发: 在最终的 Servlet 中, request 对象和中转的那个 request 是同一个对象. 
        请求的重定向: 在最终的 Servlet 中, request 对象和中转的那个 request 不是同一个对象.       
   
③. 请求的转发: 只能转发给当前 WEB 应用的的资源
      请求的重定向: 可以重定向到任何资源. 
       
④. 请求的转发: / 代表的是当前 WEB 应用的根目录
        请求的重定向: / 代表的是当前 WEB 站点的根目录. 
-->

<h2>forward_and_redirect_A</h2>
<a href="forward_B.jsp">forward_B</a>
<br><br>
<a href="redirect_B.jsp">redirect_B</a>
</body>
</html>


forward_B.jsp:

<%@ 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>

<%
request.getRequestDispatcher("/forward_and_redirect_C.jsp").forward(request, response);
//request.getRequestDispatcher("http://baidu.com").forward(request, response);
%>

</body>
</html>


redirect_B.jsp:

<%@ 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>

<%
response.sendRedirect("forward_and_redirect_C.jsp");
//response.sendRedirect("http://baidu.com");
%>

</body>
</html>


forward_and_redirect_C.jsp:

<%@ 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>forward_and_redirect_C</h2>
</body>
</html>

0 0
原创粉丝点击