1. requestdispatcher ();
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{
Response. setcontenttype ("text/html; charset = gb2312 ");
Servletcontext SC = getservletcontext ();
Requestdispatcher RD = NULL;
RD = SC. getrequestdispatcher ("/index. jsp"); // targeted page
// If the parameter is to be followed, use request. setattribute ("parameter name", parameter );
Rd. Forward (request, response );
}
Usually used in servlet, not in JSP.
2. response. sendredirect ()
It works on the user's browser. sendredirect () can be passed with parameters, such as Servlet? Name = Frank
To the next page, and it can be redirected to different hosts, sendredirect () can be redirected to frame.
JSP file.
After redirection, the URL of the redirection page will appear in the address bar of the browser.
For example, redirection in Servlet
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{
Response. setcontenttype ("text/html; charset = gb2312 ");
Response. sendredirect ("/index. jsp ");
}
Because response is an implicit object in the JSP page, you can use
Response. sendredirect () directly implements redirection.
Note:
(1) When response. sendredirect is used, there cannot be HTML output
This is not absolute. The absence of HTML output means that HTML cannot be sent to a browser. In fact, now
The server has a cache mechanism, generally at 8 K (I mean JSP server), which means that unless you disable
If you use out. Flush () to force refresh, a small number
HTML output is also allowed.
(2) After response. sendredirect, a return statement should be followed;
We already know that response. sendredirect is switched through a browser, so only on the page
After the operation is completed, the actual action will take place. Now that you have to turn, what is the significance of the subsequent output?
In addition, the redirection may fail due to the subsequent output.
Comparison:
(1) Dispatcher. Forward () is the direction of control in the container, not in the address bar of the client browser.
The address after the redirection is displayed;
(2) response. sendredirect () is a complete jump, the browser will get the jump address, and
Send a new request link. In this way, the link address after the jump is displayed in the address bar of the browser.
The former is more efficient. When the former can meet the needs, try to use requestdispatcher. Forward ()
Method.
Note: in some cases, to jump to a resource on another server, you must use
Httpservletresponse. sendrequest () method.
3. <JSP: Forward page = ""/>
The underlying part is implemented by requestdispatcher, so it has
Mark of the requestdispatcher. Forward () method.
If there are many outputs before <JSP: Forward>, the preceding output will automatically output to the customer if the buffer is full.
This statement does not work, so pay special attention to this.
Note: The browser address cannot be changed. Refresh will cause repeated submission.
4. Modify the location attribute of the HTTP header to redirect
You can directly modify the address bar to redirect the page.
The JSP file code is as follows:
<%
Response. setstatus (httpservletresponse. SC _moved_permanently );
String newlocn = "/newpath/JSA. jsp ";
Response. setheader ("location", newlocn );
%>
5. When a page is displayed in JSP for several seconds, it is automatically redirected to another page.
In the HTML file, the following code:
<Meta http-equiv = "refresh" content = "300; url = target. jsp">
Its meaning: after five minutes, the page automatically becomes target.html. Code
300 is the refresh delay time, in seconds. Targer.html: The target page you want to switch.
Refresh this page.
As shown in the preceding figure, you can use setheader to automatically redirect a page to another page after several seconds.
Key code:
String content = staytime + "; url =" + URL;
Response. setheader ("refresh", content );