There are three methods to redirect from a JSP page:
- response.sendRedirect();
- response.setHeader("Location","");
- < 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:
- java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client.
- at com.caucho.server.connection.AbstractHttpResponse.sendRedirect(AbstractHttpResponse.java:558)
- ...
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:
- java.lang.IllegalStateException: forward() not allowed after buffer has committed.
- at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:134)
- at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:101)
- at com.caucho.jsp.PageContextImpl.forward(PageContextImpl.java:836)
- ...
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.
- What are servlets and common Servlet APIs?
- Automatic jump to error page for JSP Servlet instance
- 8-point discussion on optimizing JSP Servlet applications
- Servlet import event-driven technology in JSP development
- What is JSP and comparison with Servlet?