Compare three methods of JSP page Jump

Source: Internet
Author: User

There are three methods to redirect from a JSP page:

  
  
  1. response.sendRedirect();  
  2. response.setHeader("Location","");  
  3. < jsp:forward page="" /> 

The following rules are obtained after testing:

JSP page Jump Rule 1. response. sendRedirect ()

Out. flush () is not allowed before this statement. If yes, an exception occurs:

 
 
  1. java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client.  
  2. at com.caucho.server.connection.AbstractHttpResponse.sendRedirect(AbstractHttpResponse.java:558)  
  3. ... 

The browser address bar changes after the jump;

If you want to jump to different hosts, after the jump, the statement after this statement will continue to be executed, as if a new thread, but the response operation has no significance;

If you want to jump to the same host, the statement after this statement is executed will not jump;

JSP page Jump rule 2. response. setHeader ("Location ","")

No out. flush () is allowed before this statement. If yes, the page will not jump.

After the jump, the browser address bar changes. After the statement is executed, it jumps.

JSP page Jump Rule 3. <jsp: forward page = "">

Out. flush () is not allowed before this statement. If yes, an exception occurs:

 
 
  1. java.lang.IllegalStateException: forward() not allowed after buffer has committed.  
  2. at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:134)  
  3. at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:101)  
  4. at com.caucho.jsp.PageContextImpl.forward(PageContextImpl.java:836)  
  5. ... 

After the jump, the address bar of the browser remains unchanged, but the jump can only be to the current host. After the statement is executed, the jump will be completed.

  1. What are servlets and common Servlet APIs?
  2. Automatic jump to error page for JSP Servlet instance
  3. 8-point discussion on optimizing JSP Servlet applications
  4. Servlet import event-driven technology in JSP development
  5. What is JSP and comparison with Servlet?

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.