Comparison of five JSP jump methods

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 servlet
 
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
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 is uploaded to the next page, and can be redirected to different hosts. sendRedirect () can be used to redirect jsp files with frame.

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 () in the jsp page to directly implement relocation.

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, the current server has a cache mechanism, generally at 8 K (I mean JSP SERVER), which means that unless you disable the cache or you use out. flush () force refresh. A small amount of HTML output is allowed before sendRedirect is used.

(2) After response. sendRedirect, a return statement should be followed;

We already know that response. sendRedirect is switched through a browser, so there will be actual actions only after Page processing is complete. 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 redirection of control in the container. The address bar of the browser on the client does not display the redirection address;

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.