Several servlet page jumping methods.

Several servlet page jumping methods.

Review the principle knowledge

 

   

servlet

1) The redirect method
response.sendRedirect("/a.jsp");
the path of the page is a relative path. sendRedirect can jump the page to any page, not necessarily limited to this web application, such as:
response.sendRedirect(" http://www.ycul.com");

The browser address bar changes after the jump.
If you want to pass the value out in this way, you can only use the parameter in the url or put it in the session, and 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 the page in this web application.

The browser address bar will not change after the jump.
Using this method to jump, you can use three methods to pass the value: parameter, session, request.setAttribute in the url


JSP:
1) response.sendRedirect();
Same as response.sendRedirect() of servlet.

Out.flush() is not allowed before this statement, 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)
...
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 was opened, but the response to the response The operation is meaningless;
if you want to jump to the same host, you will not jump until the statement following this statement is executed;


2) response.setHeader("Location","");
out.flush() is not allowed before this statement, if there is, the page will not jump.
After the jump, the browser address bar changes
. The statement after this statement will not jump until the execution of the statement is completed.

3) <jsp:forward page="" />
This statement is not allowed to have out.flush(), if there is, there will be an exception:
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 browser address bar does not change after the jump, but it can only jump to the current host, and
the jump will only be done after the statement following this statement is executed.

Note: When using the above page jump method in doget and post, you don't need to use the out object to output any content to the page, otherwise an error will be reported! ! ! Cannot forward after response has been committed

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326333292&siteId=291194637