Difference between getRequestDispatcher () and sendRedirect (), getrequestdispatcher

Source: Internet
Author: User

Difference between getRequestDispatcher () and sendRedirect (), getrequestdispatcher

1. request. getRequestDispatcher () is request forwarding, and a request is shared between the front and back pages;

Response. sendRedirect () is a redirection, and the front and back pages are not a request.

Request. getRequestDispather (); A RequestDispatcher object is returned.

2. RequestDispatcher. forward () is run on the server;
HttpServletResponse. sendRedirect () is accomplished by sending a command to the client browser.
Therefore, RequestDispatcher. forward () is "Transparent" for the browser ";
HttpServletResponse. sendRedirect () is not.

 

3. the url in ServletContext. getRequestDispatcher (String url) can only use an absolute path.

Relative paths can be used for URLs in ServletRequest. getRequestDispatcher (String url. Because

ServletRequest has the concept of relative paths, while ServletContext does not.

 

The RequestDispatcher object obtains the request from the client and passes them to the servlet, html, or

Jsp. It has two methods:


1. void forward (ServletRequest request, ServletResponse response)
The request can be passed by one Servlet to receive the request, and the other Servlet uses the request to generate the response. Request. response is the information returned by the client. Forward is called before response arrives at the client, that is, before response body output has been flushed. If not, it reports an exception.


2. void include (ServletRequest request, ServletResponse response)
Used to record the request and response. You cannot modify the status information in response later.

 

To transfer requests to an address in another Web App, follow these steps:
1. Obtain the ServletConext object (currentServletContext. getContext (uripath) of another Web App )).

2. Call the ServletContext. getRequestDispatcher (String url) method.

Eg: ServletContext. getRequestDispatcher ("smserror. jsp"). forward (request, response );

 

Http://lanew.blog.ccidnet.com/blog-htm-do-showone-type-blog-itemid-3690834-uid-327434.html

 

Differences:
Response. sendRedirect (url) redirects to the specified URL address to generate a new request. Therefore, to pass the parameter, only add the parameter after the url
Number, such:
Url? Id = 1.
Request. getRequestDispatcher (url). forward (request, response) directly forwards the request to the specified URL.
The data of the previous request can be directly obtained, that is, the request object is always present and will not be re-created. While
SendRedirect () creates a request object, so data in the previous request is lost.
Specifically, this is the case:
Redirect will first send a response to the browser, then the browser receives the response and then sends a requeset to the server, and then
The server sends a new response to the browser. At this time, the request received by the page is sent from the browser.
Forward occurs inside the server and is sent to the response of another page of the browser without the knowledge of the browser.
The received request is not directly sent from the browser. It may have been stored in the request using request. setAttribute.
You can directly use request. getAttribute to obtain data.
The most basic usage is as follows. Other notes are as follows:
Redirect Mode
Http: // localhost: 8080/Test application
The forward method can be used to redirect only one resource in the same Web application. The sendRedirect method allows you to redirect to any
URL.
Form action = "/uu"; sendRedirect ("/uu"); indicates the root path relative to the server. For example, http: // localhost: 8080/Test should
Submitted to http: // localhost: 8080/uu );
The "/uu" in the Forward code indicates the path relative to the WEB application. For example, http: // localhost: 8080/Test application (submit
Http: // localhost: 8080/Test/uu );
(Use the Forward method of the RequestDispatcher Interface)
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 followed by parameters, such as servlet? Name = frank.
Response. setAttribute ("name", name) to pass to the next page.
The URL in the browser address bar remains unchanged after redirection.
The forward method can be called only when the client has no output. If the buffer on the current page is not empty, you are calling
You must clear the buffer before using the forward method.
"/" Indicates the path relative to the web Application
RequestDispatcher rd = request. getRequestDispatcher ("/ooo ");
Rd. forward (request, response); Submit to http: // localhost: 8080/Test/ooo
RequestDispatcher rd = getServletContext (). getRequestDispatcher ("/ooo ");
Rd. forward (request, response); Submit to http: // localhost: 8080/Test/ooo
RequestDispatcher rd = getServletContext (). getNamedDispatcher ("TestServlet"); (TestServlet is
<Servlet-name>)
Rd. forward (request, response); Submit to the servlet named TestServlet
If there are a lot of output before <jsp: forward>, and the previous output will automatically output to the client when the buffer is full, the statement will not work,
Pay special attention to this.
Note: The browser address cannot be changed. Refresh will cause repeated submission.
Forward from http: // localhost: 8080/Test/gw/page. jsp
<Jsp: forward page = "OtherPage. jsp"/> after the JSP page is parsed, it is converted to pageContext. forward ("OtherPage. jsp ");
"/OtherPage. jsp" submitted to http: // localhost: 8080/Test/OtherPage. jsp
"OtherPage. jsp" submitted to http: // localhost: 8080/Test/gw/OtherPage. jsp
(SendRedirect using the HttpServletResponse Interface) method 302
It works on the user's browser. sendRedirect () can be passed with parameters, such as servlet? Name = frank passed to the next page,
At the same time, it can be redirected to different hosts, and sendRedirect () can be redirected to jsp files with frame.
Assume that the forwarding Code contains the registered servlet-url:/ggg/tt; jsp:/ggg/tt. jsp:
Absolute path: response. sendRedirect ("http://www.brainysoftware.com
Root path: response. sendRedirect ("/ooo") sent to http: // localhost: 8080/ooo
Relative Path: response. sendRedirect ("ooo") sent to http: // localhost: 8080/Test/ggg/ooo,
SendRedirect is equivalent to this method.
Response. setStatus (HttpServletResponse. SC _MOVED_PERMANENTLY );
String newLocn = "/newpath/jsa. jsp ";
Response. setHeader ("Location", newLocn );
(Meta Refresh) Method 200
This method is provided by HTML, and Meta itself is an HTML Tag. Usage: <meta http-equiv = "refresh" content = "5;
Url = http://www.dreamdu.com/"/>
Corresponding java code
String content = stayTime + "; URL =" + URL;
Response. setHeader ("REFRESH", content );
Bytes ------------------------------------------------------------------------------------------------
--------------------------
Use response. sendRedirect () to change the address bar
The information in the address bar of request. getRequestDispatcher (). forward (request, response) remains unchanged.
Bytes ------------------------------------------------------------------------------------------------
--------------------------
Request. setAttribute
You can only use method 2 to jump to a new page.

Reprinted: http://www.cnblogs.com/phpzxh/archive/2010/02/01/1661137.html

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.