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
- Analysis of Servlet multithreading Problems
- Security multithreading in Servlet containers
- Analysis of form data in JSP Servlet Technology
- Comparison between JSP development framework and Servlet-based Tapestry
- At the beginning of JSP Servlet Development