Home > Java > javaTutorial > Several methods of page jump in java servlet

Several methods of page jump in java servlet

巴扎黑
Release: 2017-05-21 14:13:06
Original
1642 people have browsed it

java servlet Several page jump methods, friends in need can refer to

Servlet:

Of course, in servlet, general jumps occur in doGet, doPost and other methods in.

1) redirect method

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

The path of the page is a relative path. sendRedirect can jump to any page, not necessarily limited to this web application, such as:

response.sendRedirect("http://www.jb51.net");

The browser address bar changes after the jump.

If you want to pass the value in this way, you can only bring the parameter in the URL or put it in the session. You cannot use request.setAttribute to pass it.

2) forward method

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

dispatcher .forward(request, response);

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

The browser address bar will not change after the jump.

Use this method to jump, and you can use three methods to pass values: url with parameter, session, request.setAttribute

JSP:

1) response.sendRedirect ();

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

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

java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client .

at com.caucho.server.connection.AbstractHttpResponse.sendRedirect(AbstractHttpResponse.java:558)

...

Changes in the browser address bar after the jump

If you want to jump to a different host, after the jump, the statements following this statement will continue to execute, as if a new thread has been 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 execution is completed;

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

Not allowed before this statement There is out.flush(). If there is, the page will not jump.

The browser address bar changes after the jump

The jump will not occur until the statements following this statement are executed

The above is the detailed content of Several methods of page jump in java servlet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template