The difference between ForWord and Sendredirect in JSP

Source: Internet
Author: User
js|redirect|word| difference

1.requestdispatcher.forward ()

is on the server side, when using forward (), the Servlet engine passes HTTP requests from the current servlet or JSP to another servlet,jsp or normal HTML file, or your form commits to a.jsp, The forward () redirect to b.jsp is used in a.jsp, where all the information submitted by form is available in b.jsp and the parameters are automatically passed.

However, forward () cannot be redirected to a JSP file with a frame, which can be redirected to an HTML file with a frame, while forward () cannot be passed back with parameters such as Servlet?name=frank, which is not possible. You can pass Response.setattribute ("name", name) to the next page within your program.

The browser address bar URL does not change after redirection.

Example: redirection in servlet files
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 ()

is working on the user's browser side, Sendredirect () can be passed with parameters, such as Servlet?name=frank to the next page, and it can be redirected to different hosts, and the URL of the redirected page appears on the browser's address bar.

Sendredirect () can redirect the JSP file with frame.

Example: redirection in servlet files
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.