Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between java redirection and forwarding

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the difference between java redirection and forwarding", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the difference between java redirection and forwarding" article.

Define

First, let's take a look at the javadoc of both.

SendRedirect ()

/ * *

* Sends a temporary redirect response to the client using the

Specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading'/ 'the container interprets it as relative to the current request URI.

If the location is relative with a leading'/ 'the container interprets it as relative to the servlet container root.

, /

Public void sendRedirect (String location) throws IOException

A redirect is a temporary redirected response to a specified URL sent to the client.

Forward ()

/ * *

* Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

The request and response parameters must be either the same objects as were passed to the calling servlet's service method.

, /

Public void forward (ServletRequest request, ServletResponse response)

Forwarding is to transfer a request to another resource on the server. A response is generated after processing the initial request for additional resources.

The definition basically explains why the forwarding operation can keep the parameter,attribute in the request, but the redirect operation will discard it:

Forwarding is done on the server side without going through the client side.

The response is not generated until the whole operation is forwarded.

Redirection is when the server sends the specified URL to the client.

The redirection is done on the client.

Let's take a look at how the two are implemented within Tomcat.

two。 Container implementation

We are generally quite intuitive about the use of both within servlet, such as requests for hello.jsp:

SendRedirct method

Response.sendRedirect ("/ hello.jsp")

At this point, the internal processing is as follows:

Public void sendRedirect (String location, int status) throws IOException {

/ / Generate a temporary redirect to the specified location

Try {

String absolute = toAbsolute (location)

SetStatus (status); / / here, the redirect returns the 302 status code as well as Location and the corresponding url

SetHeader ("Location", absolute)

} catch (IllegalArgumentException e) {

SetStatus (SC_NOT_FOUND)

}}

That is, according to Location, the browser finally initiates a new request, and what is finally displayed in the browser is the URL of the new request, that is, the redirection, which is often said, will show the final URL.

All of this does not cause the redirect operation to throw away a series of parameter and attribute already bound in the request. The most fundamental reason is that after the complete processing of a request, the whole request will have a process of release, that is, the process of executing release in the finally block executed by the service method of CoyoteAdapter, which is basically as follows:

Finally {if (! comet & &! async | | error.get ()) {

Request.recycle (); / / notice these two lines of code

Response.recycle ()

}

}

The recycle code of a specific request is as follows:

/ * *

* Release all object references, and initialize instance variables, in preparation for reuse of this object.

, /

Public void recycle () {

Attributes.clear ()

RequestedSessionId = null

RequestedSessionURL = false

ParameterMap.clear ()

PathParameters.clear ()

} We see that the data used to store the settings of the setAttribute method and the setParameter method is clear here. This is the real reason why redirects cannot retain data.

Forward method

The forward method is generally used as follows:

Request.getRequestDispatcher ("/ hello.jsp") .forward (request, response)

The doForward method of dispatcher will eventually be called inside the forward method

Void doForward (ServletRequest request, ServletResponse response) {

/ / Set up to handle the specified request and response

State state = new State (request, response, false)

WrapResponse (state)

ApplicationHttpRequest wrequest =

(ApplicationHttpRequest) wrapRequest (state); String contextPath = context.getPath ()

HttpServletRequest hrequest = state.hrequest

If (hrequest.getAttribute (

RequestDispatcher.FORWARD_REQUEST_URI) = = null) {

Wrequest.setAttribute (RequestDispatcher.FORWARD_PATH_INFO,hrequest.getPathInfo ())

Wrequest.setAttribute (RequestDispatcher.FORWARD_QUERY_STRING, hrequest.getQueryString ();}

Wrequest.setContextPath (contextPath)

Wrequest.setRequestURI (requestURI)

Wrequest.setServletPath (servletPath)

Wrequest.setPathInfo (pathInfo)

If (queryString! = null) {

Wrequest.setQueryString (queryString)

Wrequest.setQueryParams (queryString)

}

ProcessRequest (request,response,state); / / request for the second resource

}

}

The request processing of the second resource is similar to the general request processing, except that the second request continues to be launched on top of the first request and no response is returned, and the various parameters of the first request will continue to be passed backwards. after the final data is processed, the whole response is sent back to the client. At this point, the above release process will still go, but it doesn't matter. After all, the second resource has already been requested to be processed.

Because the browser sends a request with a fixed URL, the whole redirection is carried out internally on the server, and the browser does not perceive it, so it will not be displayed.

The above is the content of this article on "what is the difference between java redirection and forwarding". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report