04-【servlet转发和重定向】

转发:

1 //forward(将 数据传给下一个资源(servlet,jsp ,html等 ,把请求和响应的数据和参数设置带过去 )
2 request.getRequestDispatcher("/ok.jsp").forward(request, response);

重定向

1 //重定向到 jsp页面 
2 response.sendRedirect(request.getContextPath()+"/redirect.jsp");
3 response.sendRedirect("http://www.baidu.com");

servlet可以直接跳转到一个页面:比如ok.jsp页面
转发和重定向 比 printwriter.print("")好, 不需要在servlet中拼装html代码 ,我们只需要关注数据
 请求转发和重定向的区别
> 请求转发是一个请求一次响应,而重定向是两次请求两次响应
> 请求转发地址栏不变化,地址栏会发生改变【重定向会显示后一个请求的地址】
> 请求转发只能转发到本项目其他Servlet,而重定向不只能重定向到本项目的其他Servlet,还能定向到其他项目
> 请求转发是服务器端行为,只需给出转发的Servlet路径,而重定向需要给出requestURI,即包含项目名!
> 请求转发和重定向效率是转发高!因为是一个请求!
  <> 需要地址栏发生变化,那么必须使用重定向!
  <> 需要在下一个Servlet中获取request域中的数据,必须要使用转发!
>重定向的第二个请求一定是GET;
>重定向 无法获取 request对象中共享的数据, 因为是两次请求和响应 ,request对象是一次请求和响应中共享request对象
代码解释:
转发到ok.jsp

 1 <%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'ok.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21 
22   </head>
23   
24   <body>
25    ${name } This is ok.JSP page....... <br>
26   </body>
27 </html>
View Code

转发的测试java代码:TestRequestDispatcher.java

 1 package boom.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 /**
10  * 测试请求转发:<url-pattern>/TestRequestDispatcher
11  * 
12  * @author Administrator
13  *
14  */
15 public class TestRequestDispatcher extends HttpServlet {
16     @Override
17     protected void service(HttpServletRequest request, HttpServletResponse response)
18             throws ServletException, IOException {
19         // request存放数据
20         request.setAttribute("name", "servlet中设置存放的数据");
21         // 转发到ok.jsp页面
22         //forward(将 数据传给下一个资源(servlet,jsp ,html等 ,把请求和响应的数据和参数设置带过去  )
23         request.getRequestDispatcher("/ok.jsp").forward(request, response);
24     }
25 }
View Code

重定向到redirect.jsp

 1 <%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'redirect.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     这是重定向的测试页面This is redirect.jsp page. <br>
27   </body>
28 </html>
View Code

重定向的测试java代码:TestRedirect.java

 1 package boom.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 /**
10  * 测试重定向:/TestRedirect
11  * @author Administrator
12  *
13  */
14 public class TestRedirect extends HttpServlet {
15     @Override
16     protected void service(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18         //request中存放数据
19         request.setAttribute("name", "测试重定向的数据");
20         //重定向到 jsp页面 
21         //response.sendRedirect(request.getContextPath()+"/redirect.jsp");
22         response.sendRedirect("http://www.baidu.com");
23         
24         //重定向的第二个请求一定是GET
25         String method = request.getMethod();
26         System.out.println(method);
27     }
28 }
View Code
原文地址:https://www.cnblogs.com/cao-yin/p/9885382.html