Introduction to the Forward and sendRedirect methods of JSP

Source: Internet
Author: User

Response. sendRedirect is two different requests that are redirected on the client, and the browser displays the name of the page after the jump. Because it redirects from the client to the second page.

When forward is used, because only one request is sent, the property setAttribute set for the request can still be stored on the next page.

When sendRedirect is used, the request attribute cannot be obtained next time because two request requests are sent. However, you can rewrite the URL.

The content is passed in.

The following is an example:

Tian. jsp
<% Request. setAttribute ("river", "redwater ");
Request. setAttribute ("country", "daguaiwan ");
%>
<Jsp: forward page = "fengshan. jsp"/>
Donglan. jsp
<% Request. setAttribute ("river", "redwater ");
Request. setAttribute ("country", "daguaiwan ");
%>
<A href = "fengshan. jsp"> fengshan </a>
  Bama. jsp
<% Request. setAttribute ("river", "redwater ");
Request. setAttribute ("country", "daguaiwan ");
Response. sendRedirect ("fengshan. jsp ");
%>
Fengshan. jsp
<% = Request. getAttribute ("river") %> <br/>
<% = Request. getAttribute ("country") %>
 
   
 
  1. Http: // localhost: 8080/ForwardRedirect/tiane. jsp:
  2. Redwater
  3. Daguaiwan
  4. Http: // localhost: 8080/ForwardRedirect/donglan. jsp:
  5. Null
  6. Null
  7. Http: // localhost: 8080/ForwardRedirect/bama. jsp:
  8. Null
  9. Null

Result Analysis: tiane. jsp jumps to fengshan through forward. jsp, the two pages use the same request, so fengshan. jsp can receive tiane. attributes set by jsp through request. And donglan. jsp sends another request through href, bama. jsp jumps through sendRedirect and uses another request. Therefore, fengshan. jsp cannot receive the attributes set by request on these two pages. Jump between Jsp and Servlet: jump from Jsp to Servlet can be used or response. sendRedirect (URL), the difference between them has been described above. Jump from Servlet to Jsp: getRequestDispatcher's forward (request, response) method. Here we only send a request, and you can accept the attributes set by the request on the next page. 2. response. sendRedirect (URL). Two requests are sent here. The attributes set by the request cannot be accepted on the next page.

Example: servlettiane. jsp

   
 
  1. <%request.setAttribute("river","redwater");   
  2.     request.setAttribute("country","daguaiwan");   
  3. %>   
  4. <jsp:forward page="/tiane" />   
  5.  
  6. servletbama.jsp      
  7. <%request.setAttribute("river","redwater");   
  8.     request.setAttribute("country","daguaiwan");   
  9.     response.sendRedirect("/ForwardRedirect/tiane");   
  10. %>  

Tiane. java

   
 
  1. public class Tiane extends HttpServlet {   
  2.  
  3.   public void doGet(HttpServletRequest request, HttpServletResponse response)   
  4.       throws ServletException, IOException {   
  5. //    response.sendRedirect("/ForwardRedirect/fengshan.jsp");   
  6.     this.getServletConfig().getServletContext()   
  7.             .getRequestDispatcher("/fengshan.jsp").forward(request, response);   
  8.   }   
  9.  
  10.   public void doPost(HttpServletRequest request, HttpServletResponse response)   
  11.       throws ServletException, IOException {   
  12.     doGet(request, response);   
  13.   }      
  14. }  

Configure servlet in web. xml:

   
 
  1. <servlet>   
  2.   <servlet-name>tiane</servlet-name>   
  3.   <servlet-class>com.dvnchina.test.Tiane</servlet-class>   
  4.   </servlet>   
  5.   <servlet-mapping>   
  6.   <servlet-name>tiane</servlet-name>   
  7.   <url-pattern>/tiane</url-pattern>   
  8. </servlet-mapping>  

I will not analyze the specific results, which is similar to the first example. See the attachment for the code.

  1. Analysis on CGI supported by jsp http Server
  2. Use JSP pages to generate PDF reports
  3. Step for customizing JSP labels
  4. Detailed test of JSP containers
  5. Describes the following features of jsp http Server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.