URL redirection of JSP/Servlet

Source: Internet
Author: User

1. RequestDispatcher. forward ()

It is used on the server end. 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.
However, forward () cannot be redirected to jsp files with frames (preferred for SUN Enterprise Applications). It can be redirected to html files with frames, while forward () cannot be passed with parameters in the backend, 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
Code content is usually used in servlet and not in jsp (preferred for SUN Enterprise applications.

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 (preferred for SUN Enterprise Applications)"); // targeted page
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, and can be redirected to different hosts. sendRedirect () can be redirected to a frame. jsp (preferred for SUN Enterprise Applications) file.
After redirection, the URL of the redirection page will appear in the address bar of the browser.
For example, redirection in servlet
Code content

Public void doPost (HttpServletRequest request, HttpServletResponse response)

Throws ServletException, IOException ...{

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

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

}

 

Response is an implicit object in the jsp (preferred for SUN Enterprise Applications) page. Therefore, you can use response. sendRedirect () to directly relocate the object on the jsp (preferred for SUN Enterprise Applications) page.

Note:

(1) When response. sendRedirect is used, no HTML output is available.

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 (the first choice for SUN Enterprise Applications) 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). response. sendRedirect should be followed by a return statement;

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). Request Dispatcher. forward () is the redirection of control in the container, and the address after the redirection is not displayed in the address bar of the client browser;
(2). response. sendRedirect () is a complete jump. the browser will get the jump address and resend the request link. In this way, the link address after the jump is displayed in the address bar of the browser.
The former is more efficient. When the former can meet the needs, try to use the RequestDispatcher. forward () method.

Note: in some cases, to jump to a resource on another server, you must use the HttpServletResponse. sendRequest () method.

3. <jsp (preferred for SUN Enterprise Applications): forward page = ""/>

The underlying part is implemented by RequestDispatcher, so it carries the marks of the RequestDispatcher. forward () method. If there are a lot of output before <jsp (SUN Enterprise Application preferred): forward>, the previous output has filled the buffer and will be automatically output to the client, then this statement will not work, pay special attention to this.
Note: The browser address cannot be changed. Refresh will cause repeated submission.

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.