1. requestdispatcher. Forward ()
Is used on the server. When forward () is used, the servlet engine transmits the HTTP request from the current servlet or JSP to another servlet, JSP or common HTML file, that is, your form is submitted to. JSP, in. JSP uses forward () to redirect to B. JSP. At this time, all information submitted by form is in B. JSP can be obtained, and parameters are automatically transmitted.
However, forward () cannot be redirected to a JSP file with a frame. It can be redirected to an HTML file with a frame. At the same time, forward () cannot be passed with parameters after it, such as Servlet? Name = Frank. If this is not the case, you can use response. setattribute ("name", name) in the program to upload it to the next page.
The URL in the browser address bar remains unchanged after redirection.
For example, redirection in a Servlet File
Code
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 ");
Rd. Forward (request, response );
}
2. response. sendredirect ()
It works on the user's browser. sendredirect () can be passed with parameters, such as Servlet? Name = Frank is uploaded to the next page. At the same time, it can be redirected to different hosts, and the URL of the redirected page will appear in the address bar of the browser.
Sendredirect () can redirect JSP files with frames.
For example, redirection in a Servlet File
Code
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{
Response. setcontenttype ("text/html; charset = gb2312 ");
Response. sendredirect ("/index. jsp ");
}