Java servlets several ways to jump pages

  • 2020-04-01 01:39:15
  • OfStack

The Servlet:

Of course, in servlets, jumps generally occur in methods like doGet, doPost, etc.

1) the redirect

The response. SendRedirect ("/a. sp ");

The path of the page is a relative path. SendRedirect can redirect a page to any page and is not necessarily limited to the web application, such as:

The response. SendRedirect (" / / www.jb51.net ");

The browser address bar changes after the jump.

If the value is to be passed out in this way, it can only be passed with a parameter in the url or in the session, and cannot be passed by request-setattribute.

2) the way forward

RequestDispatcher dispatcher = request. GetRequestDispatcher ("/a. sp ");

The dispatcher. Forward (request, response);

The path of the page is a relative path. Forward mode can only jump to the page in this web application.

The browser address bar does not change after the jump.

In this way, there are three methods to pass values: parameter, session, and request-setattribute in the url

The JSP:

1) the response. SendRedirect ();

The same way a servlet does response.sendredirect ().

Out.flush () is not allowed before this statement. If so, there will be an exception:

Java. Lang. An IllegalStateException: Can 't sendRedirect () after the 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 a different host, after the jump, the statement after this statement will continue to execute, as if a new thread is opened, but the operation of response is meaningless;

If you want to jump to the same host, the statement after this statement will not jump until the completion of the execution;

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

Out.flush () is not allowed before this statement, and if so, the page does not jump.

The browser address bar changes after the jump

The statement that follows this statement will not jump until it is completed


Related articles: