Differences between Servlet page Jump implementation methods

Source: Internet
Author: User

I have been confused about several methods of Servlet page Jump. I searched the internet today and found it again. I'll take a look later.

The Servlet page Jump is divided into two parts: Servlet, JSP, and servlet.

Servlet:

Of course, in servlet, the jump usually occurs in methods such as doGet and doPost.

1) redirect Mode

Response. sendRedirect ("/a. jsp ");

The path of the page is relative. SendRedirect can jump the page to any page, which may not be limited to this web application, for example:

Response. sendRedirect ("URL ");

The browser address bar changes after the jump.

In this way, values can only be transmitted with parameter in the url or in the session. request. setAttribute cannot be used.

2) forward mode

RequestDispatcher dispatcher = request. getRequestDispatcher ("/a. jsp ");

Dispatcher. forward (request, response );

The Servlet page Jump path is a relative path. The forward method can only jump to the page in this web application.

The browser address bar does not change after the jump.

In this way, you can use three methods for transferring values: url with parameter, session, request. setAttribute

JSP:

1) response. sendRedirect ();

It is the same as servlet's response. sendRedirect () method.

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)

...
Browser address bar changes after jump

If the Servlet page Jump is to jump to different hosts, after the jump, the statements following this statement will continue to be executed, as if a new thread, but the response operation is meaningless;

If you want to jump to the Servlet page to jump to the same host, the following statements after the statement is executed will jump;

2) response. setHeader ("Location ","");

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

Browser address bar changes after jump

Only after the statement is executed

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)

...
The address bar of the browser remains unchanged after the Servlet page jumps, but the address bar can only jump to the current host.

Only after the statement is executed

  1. Analysis of Servlet multithreading Problems
  2. Security multithreading in Servlet containers
  3. Analysis of form data in JSP Servlet Technology
  4. Comparison between JSP development framework and Servlet-based Tapestry
  5. At the beginning of JSP Servlet Development

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.