Comparison of JSP redirection forward and sendredirect
Source: Internet
Author: User
There are two types of JSP redirection: Forward and sendredirect. What are their principles and differences? There are many differences in their use. Those are superficial phenomena. After understanding their respective principles, the differences in use can be easily grasped. I. Principle. 1. Forward is a redirection on the server side. When the server sends data to the client
In this case, the server outputs data to the buffer before sending data to the client, and then sends the data in the buffer to the client. When will the data in the buffer be sent to the client?
End? (1) When the request from the client is processed and all data is output to the buffer, (2) when the buffer is full, (3) the buffer output method is called in the program
Out. Flush () or response. flushbuffer (), Web Container sends the data in the buffer to the client. This redirection method uses the server-side buffer mechanism. before sending the data in the buffer to the client, the original data is not sent, and the execution is redirected to the redirect page to send the data on the redirect page, data on the Redirect call page will be cleared. If there are a lot of output before <JSP: forword>, and the previous output will automatically output to the client when the buffer is full, this redirection method will not work, so pay special attention to this. Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
{
Response. setcontenttype ("text/html; charset = UTF-8 ");
Servletcontext SC = getservletcontext ();
Requestdispatcher RD = NULL;
RD = SC. getrequestdispatcher ("/index. jsp ");
Rd. Forward (request, response );
} 2. sendredirect is a redirection process on the client. This method modifies the HTTP header to issue a redirection command to the browser, so that the browser can request the URL specified in the location to display the content of the redirected webpage. This method can accept absolute or relative URLs. If the parameter passed to this method is a relative URL, Web Container converts it into an absolute URL before sending it to the client. Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
{
Response. setcontenttype ("text/html; charset = UTF-8 ");
Response. sendredirect ("/index. jsp ");
}
Ii. Differences. 1. Forward redirection is the redirection of the same web application implemented in the container. Therefore, the forward method can only redirect to one resource in the same web application. After the redirection, the URL in the browser address bar remains unchanged, the sendredirect method can redirect to any URL, because this method is implemented by modifying the HTTP header. There is no restriction on the URL. After the redirection, the URL in the browser's address bar changes. 2. Forward redirection transmits the original HTTP request object (request) from one servlet instance to another instance, while sendredirect is not the same application. 3. Based on the second point, the parameter transmission method is different. The form parameter of forward is passed along, so the parameters of the HTTP request can be obtained in the second instance. Sendredirect can only pass parameters through links, response. sendredirect ("login. jsp? Param1 = "). 4. sendredirect can process relative URLs and automatically convert them into absolute URLs. if the address is relative, there is no '/', the Web iner considers it relative to the current request URI. For example, if it is response. sendredirect ("login. JSP "), login will be searched from the URL path of the current servlet. JSP: http: // 10.1.18.8: 8081/DMS/servlet redirect URL: http: // 10.1.18.8: 8081/DMS/servlet/login. JSP. If it is response. sendredirect ("/login. JSP "), the URL: http: // 10.1.18.8: 8081/login will be searched from the current application path. JSP. Forward cannot process relative paths in this way.
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.