Difference between forward and sendredirect in JSP

Source: Internet
Author: User
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 ");

}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.