---
title: "forword和sendRedirect"
author: "fyd222"
date: "2012-07-12T10:55:02Z"
tags: []
categories: ["企业架构"]
slug: "forword-sendredirect"
draft: false
lastmod: "2012-07-12T10:55:02Z"
id: 1586040
url: "https://fyd222.iteye.com/blog/1586040"
---

jsp中forword和sendRedirect的区别

RequestDispatcher.forward()&lt;/P&gt;

是在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个Servlet,JSP 或普通HTML文件,也即你的form提交至a.jsp,在a.jsp用到了forward()重定向至b.jsp,此时form提交的所有信息在 b.jsp都可以获得,参数自动传递.&lt;/P&gt;

但forward ()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,同时forward()无法在后面带参数传递,比如 servlet?name=frank,这样不行,可以程序内通过response.setAttribute("name",name)来传至下一个页面.&lt;/P&gt;

重定向后浏览器地址栏URL不变.&lt;/P&gt;

例:servlet文件中重定向  
CODE&lt;/P&gt;

public void doPost(HttpServletRequest request,HttpServletResponse response)&lt;/P&gt;

       throws ServletException,IOException&lt;/P&gt;

{&lt;/P&gt;

       response.setContentType("text/html; charset=gb2312");&lt;/P&gt;

       ServletContext sc = getServletContext();&lt;/P&gt;

       RequestDispatcher rd = null;&lt;/P&gt;

       rd = sc.getRequestDispatcher("/index.jsp");&lt;/P&gt;

       rd.forward(request, response);  
}&lt;/P&gt;

 

2.response.sendRedirect()&lt;/P&gt;

是在用户的浏览器端工作,sendRedirect()可以带参数传递,比如servlet?name=frank传至下个页面,同时它可以重定向至不同的主机上,且在浏览器地址栏上会出现重定向页面的URL.&lt;/P&gt;

sendRedirect()可以重定向有frame的jsp文件.&lt;/P&gt;

例:servlet文件中重定向  
CODE&lt;/P&gt;

public void doPost(HttpServletRequest request,HttpServletResponse response)&lt;/P&gt;

       throws ServletException,IOException&lt;/P&gt;

{&lt;/P&gt;

       response.setContentType("text/html; charset=gb2312");&lt;/P&gt;

       response.sendRedirect("/index.jsp");&lt;/P&gt;

}  
&lt;/P&gt;

在Java Web开发中，经常会用到跳转页面的方法，一般有下面两种方法。  
Java代码

   1. HttpServletResponse response = new HttpServletResponse();   
   2. response.sendRedirect(location) 

  HttpServletResponse response = new HttpServletResponse();  
  response.sendRedirect(location)

Java代码

   1. RequestDispatcher rd = new RequestDispatcher();   
   2. rd.forward(request, response)  

  RequestDispatcher rd = new RequestDispatcher();  
  rd.forward(request, response)

   1. 跳转方式  
      [http://localhost:8080/Test](http://localhost:8080/Test)应用  
      运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可以让你重定向到任何URL。  
      表单form的action= "/uu ";sendRedirect( "/uu ");表示相对于服务器根路径。如[http://localhost:8080/Test](http://localhost:8080/Test)应用（则提交至[http://localhost:8080/uu](http://localhost:8080/uu)）;  
      Forward代码中的 "/uu "则代表相对与WEB应用的路径。如[http://localhost:8080/Test](http://localhost:8080/Test)应用（则提交至[http://localhost:8080/Test/uu](http://localhost:8080/Test/uu)）;  
   2. （运用RequestDispatcher接口的Forward）方法  
      forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,  
      同时forward()无法在后面带参数传递,比如servlet?name=frank,这样不行,可以程序内通过response.setAttribute( "name ",name)来传至下一个页面.  
      重定向后浏览器地址栏URL不变.

      只有在客户端没有输出时才可以调用forward方法。如果当前页面的缓冲区（buffer）不是空的，那么你在调用forward方法前必须先清空缓冲区。  
      "/ "代表相对与web应用路径

      RequestDispatcher   rd   =   request.getRequestDispatcher( "/ooo ");  
      rd.forward(request,   response);提交至[http://localhost:8080/Test/ooo](http://localhost:8080/Test/ooo)

      RequestDispatcher   rd   =   getServletContext().getRequestDispatcher( "/ooo ");  
      rd.forward(request,   response);提交至[http://localhost:8080/Test/ooo](http://localhost:8080/Test/ooo)

      RequestDispatcher   rd   =getServletContext().getNamedDispatcher( "TestServlet ");（TestServlet为一个 &lt;servlet-name&gt; ）  
      rd.forward(request,   response);提交至名为TestServlet的servlet

      如果在 &lt;jsp:forward&gt; 之前有很多输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起作用,这一点应该特别注意。  
      另外要注意：它不能改变浏览器地址，刷新的话会导致重复提交  
      从[http://localhost:8080/Test/gw/page.jsp](http://localhost:8080/Test/gw/page.jsp)中转发  
      &lt;jsp:forward   page= "OtherPage.jsp "/&gt; 在JSP页面被解析后转换成pageContext.forward( "OtherPage.jsp ");  
      "/OtherPage.jsp "提交到[http://localhost:8080/Test/OtherPage.jsp](http://localhost:8080/Test/OtherPage.jsp)  
      "OtherPage.jsp "提交到[http://localhost:8080/Test/gw/OtherPage.jsp](http://localhost:8080/Test/gw/OtherPage.jsp)

      (运用HttpServletResponse接口的sendRedirect）方法302  
      是在用户的浏览器端工作,sendRedirect()可以带参数传递,比如servlet?name=frank传至下个页面,  
      同时它可以重定向至不同的主机上,sendRedirect()可以重定向有frame.的jsp文件.

      假设转发代码包含于注册的servlet－url为/ggg/tt;jsp为/ggg/tt.jsp:  
      绝对路径：response.sendRedirect( "[http://www.brainysoftware.com](http://www.brainysoftware.com/) ")发送至[http://www.brainysoftware.com](http://www.brainysoftware.com/)  
      根路径：response.sendRedirect( "/ooo ")发送至[http://localhost:8080/ooo](http://localhost:8080/ooo)  
      相对路径：response.sendRedirect( "ooo ")发送至[http://localhost:8080/Test/ggg/ooo](http://localhost:8080/Test/ggg/ooo),

      sendRedirect等同于此方式  
      response.setStatus(HttpServletResponse.SC\_MOVED\_PERMANENTLY);  
      String   newLocn   =   "/newpath/jsa.jsp ";  
      response.setHeader( "Location ",newLocn);

      (Meta   Refresh)方法200  
      这种方法是由HTML提供的，Meta本身就是HTML标签。使用方法是： &lt;meta   http-equiv= "refresh "   content= "5;   url=http://www.dreamdu.com/ "   /&gt;  
      相应的java代码  
      String   content=stayTime+ ";URL= "+URL;  
      response.setHeader( "REFRESH ",content);  
   3. 使用response.sendRedirect()地址栏将改变  
      使用request.getRequestDispatcher().forward(request,response)地址栏中的信息保持不变  
   4. request.setAttribute存的东西  
      只用通过方法2跳转   才能在新页取出来  
   5. redirect   会首先发一个response给浏览器,   然后浏览器收到这个response后再发一个requeset给服务器,   然后服务器发新的response给浏览器.   这时页面收到的request是一个新从浏览器发来的.  
      forward   发生在服务器内部,   在浏览器完全不知情的情况下发给了浏览器另外一个页面的response.   这时页面收到的request不是从浏览器直接发来了,可能己经放了数据.  
      所以:  
              request.setAttribute存的东西  
              只用通过方法2跳转   才能在新页取出来