星空网 > 软件开发 > Java

举例说明重定向和请求转发的区别

重定向

  HttpServletResponse对象的sendRedirect(java.lang.String location)方法称作重定向

  如果location地址前面加上“/”,则表示相对于Servlet容器的根来请求,比如http://localhost:8080;如果location地址前面没有加上“/”,则表示相对于当前请求的URI来寻找地址。

 

请求转发

  RequestDispatcher的:forward(ServletRequest request, ServletResponse response)方法叫做请求转发。

  

 

实验例子1:重定向和请求转发似乎都是造成页面跳转

  第一个页面first.jsp:

举例说明重定向和请求转发的区别

举例说明重定向和请求转发的区别举例说明重定向和请求转发的区别
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <base href="<%=basePath%>">    <title>My JSP 'first.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <!--  <link rel="stylesheet" type="text/css" href="styles.css">  --> </head>  <body>  <form action="Second">  <input type="text" name="username">  <input type="submit" value="submit">  </form> </body></html>
举例说明重定向和请求转发的区别


 

  第二个页面是Servlet:

  用请求转发:

 

举例说明重定向和请求转发的区别
package com.shengqishiwind.servlet;import java.io.IOException;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Second extends HttpServlet{  public void doGet(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException  {    process(request, response);  }  public void doPost(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException  {    process(request, response);  }  private void process(HttpServletRequest request,      HttpServletResponse response) throws ServletException, IOException  {    // 请求转发    RequestDispatcher rd = request.getRequestDispatcher("third.jsp");    rd.forward(request, response);  }}
举例说明重定向和请求转发的区别

 

  用重定向,则把处理方法改为:

举例说明重定向和请求转发的区别
  private void process(HttpServletRequest request,      HttpServletResponse response) throws ServletException, IOException  {    // 重定向    response.sendRedirect("third.jsp");  }
举例说明重定向和请求转发的区别

 

  第三个页面是third.jsp

举例说明重定向和请求转发的区别
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <base href="<%=basePath%>">    <title>My JSP 'third.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <!--  <link rel="stylesheet" type="text/css" href="styles.css">  --> </head>  <body>  This is my Third page. <br>    </body></html>
举例说明重定向和请求转发的区别

 

  不管用请求转发还是重定向的方法,第一个页面点击提交后,都能顺利转到第三个页面:

举例说明重定向和请求转发的区别

  但是其实实际进行的操作还是很不同的,看下面的例子。

 

 

实验例子2:如果要在第三个页面中取得第一个页面输入的用户名

  请求转发的实现比较简单,第二个页面的Servlet代码不用添加任何东西,可以直接从第三个页面getParameter,即把third.jsp中的body改为:

 

 <body>  This is my Third page. <br>    用户名:<%=request.getParameter("username") %>   </body>

  则在第一个页面输入shengqishiwind,提交后,第三个页面显示:

举例说明重定向和请求转发的区别

  重定向应该怎么实现获取第一个页面提交的用户名呢?

  第三个页面采用如上的代码(通过getParameter获取参数),把第二个页面中改为重定向的跳转方式,重新来一次,可以看到显示:

举例说明重定向和请求转发的区别

  再尝试第二个页面这样(通过setAttribute存储值):

举例说明重定向和请求转发的区别
  private void process(HttpServletRequest request,      HttpServletResponse response) throws ServletException, IOException  {    String username = request.getParameter("username");    // 重定向    request.setAttribute("username", username);    response.sendRedirect("third.jsp");  }
举例说明重定向和请求转发的区别

  然后第三个页面这样获取:

   用户名:<%=request.getAttribute("username") %>  

  仍然是不行,得到的仍然是null值。

 

 

重定向和请求转发的区别

请求转发:

  RequestDispatcher是通过调用HttpServletRequest对象的getRequestDispatcher()方法得到的,是属于请求对象的方法。

  请求转发,整个过程处于同一个请求当中。 

  不管经历了多少组件,都可以从request中直接取得想要的值。

  使用请求转发时,到结果页面的网址是:http://localhost:8080/HelloWeb/Second?username=wind

 

重定向: 

  sendRedirect()是HttpServletResponse对象的方法,即响应对象的方法。

  既然调用了响应对象的方法,那就表明整个请求过程已经结束了,服务器开始向客户端返回执行的结果。

  sendRedirect调用后会向客户端返回一个响应,这个响应告诉客户端要转向的页面,紧接着客户端又会发送一个新的请求,转向这个目标页面。

  也即是说,重定向的过程中实际上客户端会向服务器发送两个请求。

 

  使用重定向时,结果页面的网址是:http://localhost:8080/HelloWeb/third.jsp

  说明客户端已经知道了结果页面的地址,是重新发送的全新的请求。

  重定向方式在Firebug中的图:

举例说明重定向和请求转发的区别

 

 

  总结记忆请求转发只有一个请求,所以勇往直前,调用的方法叫forward;而重定向需要客户端重新发送请求,调用的是sendRedirect,名字里有个Re,表示重复。




原标题:举例说明重定向和请求转发的区别

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

sky国际快递:https://www.goluckyvip.com/tag/100733.html
国际快递可寄手机:https://www.goluckyvip.com/tag/100734.html
国际货运海运价格:https://www.goluckyvip.com/tag/100735.html
国际货运从业资格证:https://www.goluckyvip.com/tag/100737.html
专业国际物流:https://www.goluckyvip.com/tag/100738.html
Square:https://www.goluckyvip.com/tag/10074.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流