JSP and servlet Several page jumps the difference __jsp

Source: Internet
Author: User
Tags flush

Servlet:
Of course, in the servlet, the general jump occurs in Doget, Dopost and other methods.
1) Redirect Way
Response.sendredirect ("/a.jsp");
The path to the page is the relative path. Sendredirect can jump to any page, not necessarily limited to this web application, such as:
Response.sendredirect ("http://www.ycul.com");

After the Jump browser address bar changes.
This way to pass the value out, you can only in the URL with parameter or in the session, can not use Request.setattribute to pass.

2) Forward Way
RequestDispatcher dispatcher = Request.getrequestdispatcher ("/a.jsp");
Dispatcher. Forward (request, response);
The path to the page is the relative path. The forward method can only jump to a page in this Web application.

The browser address bar will not change after the jump.
Use this way to jump, the value can be used in three ways: URL with Parameter,session,request.setattribute


JSP:
1) response.sendredirect ();
Same as the Response.sendredirect () method of the servlet.

Out.flush () is not allowed before this statement, and if so, there will be an exception:
Java.lang.IllegalStateException:Can ' t Sendredirect () after data has committed to the client.
At Com.caucho.server.connection.AbstractHttpResponse.sendRedirect (abstracthttpresponse.java:558)
...
Browser address bar changes after jump
If you want to jump to a different host, after the jump, the statement after this statement will continue to execute, as the new thread, but the operation of the response has no meaning;
If you want to jump to the same host, the statement after this statement completes execution before jumping;


2) Response.setheader ("Location", "");
Out.flush () is not allowed before this statement, and if so, the page will not jump.
Browser address bar changes after jump
The statement after this statement finishes execution does not jump

3) <jsp:forward page= ""/>
Out.flush () is not allowed before this statement, and if so, there will be an exception:
Java.lang.IllegalStateException:forward () 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)
...
The browser address bar does not change after the jump, but can only jump to the current host
The statement after this statement finishes execution does not jump

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.