Difference between forword and sendRedirect in jsp

Source: Internet
Author: User

. RequestDispatcher. forward () </P>


It works on the server. When forward () is used, the Servlet engine transmits the HTTP request from the current Servlet or jsp (preferred for SUN Enterprise applications) to another Servlet, jsp (preferred for SUN Enterprise Applications) or common HTML files, that is, your form is submitted to. jsp (the first choice for SUN Enterprise applications), in. jsp (the preferred choice for SUN Enterprise Applications) uses forward () to redirect to B. jsp (the first choice for SUN enterprise-level applications). At this time, all information submitted by form is in B. jsp (preferred for SUN Enterprise Applications) can be obtained, and parameters are automatically transmitted. </P>


However, forward () cannot be redirected to a jsp file with a frame (preferred for SUN Enterprise applications), and can be redirected to an html file with a frame. At the same time, forward () cannot be passed with parameters at the end, for example, servlet? Name = frank. You can use response. setAttribute ("name", name) in the program to upload data to the next page. </P>


The URL in the browser address bar remains unchanged after redirection. </P>


For example, redirection in a servlet File
CODE </P>


Public void doPost (HttpServletRequest request, HttpServletResponse response) </P>


Throws ServletException, IOException </P>


{</P>


Response. setContentType ("text/html; charset = gb2312"); </P>


ServletContext SC = getServletContext (); </P>


RequestDispatcher rd = null; </P>


Rd = SC. getRequestDispatcher ("/index. jsp (preferred for SUN Enterprise Applications)"); </P>


Rd. forward (request, response );
} </P>

 

2. response. sendRedirect () </P>


It works on the user's browser. sendRedirect () can be passed with parameters, such as servlet? Name = frank is uploaded to the next page and can be redirected to different hosts. the URL of the redirected page appears in the address bar of the browser. </P>


SendRedirect () can redirect jsp files with frames (preferred for SUN Enterprise Applications). </P>


For example, redirection in a servlet File
CODE </P>


Public void doPost (HttpServletRequest request, HttpServletResponse response) </P>


Throws ServletException, IOException </P>


{</P>


Response. setContentType ("text/html; charset = gb2312"); </P>


Response. sendRedirect ("/index. jsp (preferred for SUN Enterprise Applications)"); </P>


}
</P>

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.