Implement page forwarding in a servlet

Source: Internet
Author: User
Tags relative

You can use the Sendredirect () method of the HttpServletResponse object if you simply want to implement a simple page jump and do not need to pass the request object.

Eg:response.sendRedirect ("web address");


If you want to implement a page jump and pass the request object: Request,getrequestdispatcher ("Web Address"). Forward (Request,response);


response.sendredirect= send address to browser side, let browser request again, this is the reason that the address bar is changed
Request.getrequestdispatcher (). forward= the control switch on the server, which is equal to jumping from one servletclass to another servletclass or JSP to continue running, finally returning all the results, So the address bar doesn't change.
The validity period of attribute and parameter in request is the reason why the attribute and parameter are lost after the first one is requested, and then the request is made again.


1.request.getrequestdispatcher () is the request forwarding, the front and back page share a request;
Response.sendredirect () is redirected, and the front and back pages are not a request.

Request.getrequestdispather (); Returns a RequestDispatcher object.

2.requestdispatcher.forward () is running on the server side;
Httpservletresponse.sendredirect () is done by sending a command to the client's browser.
So Requestdispatcher.forward () is "Transparent" to the browser;
and Httpservletresponse.sendredirect () is not.

URLs in the 3.servletcontext.getrequestdispatcher (String URL) can only use absolute paths;

The URLs in the Servletrequest.getrequestdispatcher (String URL) can use relative paths. Because

ServletRequest has a concept of relative path, while ServletContext object has no sub-concept.

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

Jsp. It has two methods:


1.void forward (servletrequest request,servletresponse response)
To pass the request, a servlet can receive the request, and another servlet uses the request to generate the response. Request passes the requested, response is the information returned by the client. Forward to be called before response arrives at the client, i.e. before response body output has been flushed. If not, it will report an exception.


2.void include (servletrequest request,servletresponse response)
It is used to record the reservation request and response, and can no longer modify the information in response to indicate the state.

If you need to transfer a request to an address in another Web app, you can do this as follows:
1. Get another web app's Servletconext object (Currentservletcontext.getcontext (Uripath)).

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

The difference:
Response.sendredirect (URL) jumps to the specified URL address, resulting in a new request, so the parameter is passed only after the URL is added
Number, such as:
Url?id=1.
Request.getrequestdispatcher (URL). Forward (Request,response) is a direct forward request to the specified URL, so the request
The ability to directly obtain the data from the last request, that is, request forwarding, the requests object is always present and is not recreated. and
Sendredirect () Creates a new request object, so the data in the previous request is lost.
More specifically, this is the case:
REDIRECT will first send a response to the browser, then the browser receives this response and then sends a requeset to the server, and then
The server sends a new response to the browser. The request received by the page is a new one from the browser.
Forward occurs inside the server and sends a response to the browser's other page without the browser's knowledge. This page
The request received was not sent directly from the browser and may have been durable request.setattribute the data in the request. On the page to go
Data can be obtained directly with Request.getattribute.
The basic usage is as above, and some of the other places to be noted are as follows:
Jump mode
Http://localhost:8080/Test applications
Use the forward method to redirect only one resource in the same Web application. And the Sendredirect method allows you to redirect to any
Url.
Form form action= "/uu"; Sendredirect ("/uu"), relative to the server root path. such as Http://localhost:8080/Test should be
(then submit to Http://localhost:8080/uu);
The "/uu" in the forward code represents the relative path to the Web App. such as Http://localhost:8080/Test application (then submit to
HTTP://LOCALHOST:8080/TEST/UU);
(using the RequestDispatcher interface Forward) method
Forward () cannot redirect to a frame-capable JSP file and can be redirected to a frame-capable HTML file.
At the same time forward () can not be passed in the back with the parameters, such as Servlet?name=frank, so no, the program through
Response.setattribute ("name", name) is passed to the next page.
The browser address bar URL does not change after redirection.
The forward method can be called only if the client does not have output. If the buffer of the current page is not empty, then you are calling the
The buffer must be emptied before the forward method.
"/" stands for relative to web app path
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 a
<servlet-name>)
Rd.forward (request, response); submit to servlet named Testservlet
If there is a lot of output before <jsp:forward>, the previous output has filled the buffer and will automatically output to the client, then the statement will not work.
This should be paid special attention.
Also note: It can not change the browser address, refresh will lead to repeated submissions
Forwarding from http://localhost:8080/Test/gw/page.jsp
<jsp:forward page= "otherpage.jsp"/> Translated into Pagecontext.forward ("otherpage.jsp") after the JSP page was parsed;
"/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
is to work in the user's browser, sendredirect () can be passed with parameters, such as Servlet?name=frank to the next page,
At the same time it can redirect to a different host, Sendredirect () can redirect the JSP file with frame.
Assume that the forwarding code is included in the registered Servlet-url of/ggg/tt;jsp for/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 way
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. The method of use is: <meta http-equiv= "Refresh" content= "5;
url=http://www.dreamdu.com/"/>
The corresponding Java code
String content=staytime+ "; Url= "+url;
Response.setheader ("REFRESH", content);
------------------------------------------------------------------------------------------------
--------------------------
Using the Response.sendredirect () address bar will change
Use the information in the Request.getrequestdispatcher (). Forward (Request,response) address bar to remain unchanged.
------------------------------------------------------------------------------------------------
--------------------------
Request.setattribute the things that exist
Only jump through Method 2 to get out of the new page

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.