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!