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.

Javascript: Return big (this) "alt =" flowchart "src ="/data/attachment/portal/et2/201207/et34910080648491.jpg ">

When forward is used, because only one request is sent, the property set for the request (setattribute) 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 = http://developer.51cto.com/art/200907/ "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> 


Related Article

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.