Difference between Forward and Redirect in common JAVA interview questions, forwardredirect

Source: Internet
Author: User

Difference between Forward and Redirect in common JAVA interview questions, forwardredirect

The user sends an HTTP request to the server, which may be returned to the user after being processed by multiple information resources. Each information resource uses the request forwarding mechanism to forward requests to each other, however, the user does not feel request forwarding. Based on different forwarding methods, Direct Request Forwarding (Forward) and indirect request Forwarding (Redirect) can be distinguished. What are the differences between the two forwarding methods? This article provides a comprehensive explanation of the Principles and differences between the two request forwarding methods.

[Occurrence frequency]

[Key test sites]

  • Meaning of request forwarding;
  • Principles of Forward requests;
  • The principle of Redirect request forwarding.

[Question analysis]

Forward and Redirect represent two request forwarding Methods: Direct forwarding and indirect forwarding.

Direct Forwarding (Forward)The client and browser send a request only once. Servlet, HTML, JSP, or other information resources are responded to by the second information resource. In the request object request, the saved object is shared for each information resource.

  Indirect forwarding method (Redirect)It is actually two HTTP requests. When the server responds to the first request, the browser sends a request to another URL to forward the request.

For example:

 Direct Forwarding is equivalent to: "A asks B to borrow money, B says no, B goes to C to borrow, and if B does not borrow it, the message will be transmitted to ";

Indirect Forwarding is equivalent to: "A borrow money from B, B says no, let A borrow money from C ".

The principles of the two are described in detail below:

I. Indirect request Forwarding (Redirect)

The indirect forwarding method is also called redirection. It is generally used to avoid abnormal user access. For example, if a user does not log on to the backend resource, the Servlet can redirect the HTTP request to the logon page so that the user can access the resource after logon. In Servlet, The SendRedirect () method of the response object is called to tell the browser to redirect access to the specified URL. The sample code is as follows:

...... // Servlet's method for processing get requests public void doGet (HttpServletRequest request, HttpServletResponse response) {// redirect the request to another resource response. sendRedirect ("resource URL ");}........

The indirect request forwarding process is as follows:

Ii. Direct Request Forwarding (Forward)

Direct forwarding uses more methods. Generally, request forwarding refers to direct forwarding. Most Web applications have a controller. The controller controls the information resource to which the request should be forwarded. The request is then processed by these information resources. After the request is processed, it may be forwarded to another information resource and returned to the user. This process is a classic MVC model.

Javax. serlvet. the RequestDispatcher interface is an interface that must be implemented by the request forwarder. The Web Container provides the Servlet with an object to implement this interface and calls the forward () method of this interface to forward requests, the sample code is as follows:

...... // Servlet public void doGet (HttpServletRequest request, HttpServletResponse response) {// gets the request forwarder object, which points to the object through getRequestDisPatcher () set RequestDispatcher requestDispatcher = request. getRequestDispatcher ("resource URL"); // call the forward () method to forward the request requestDispatcher. forward (request, response );}
......

The Direct Request forwarding process is as follows:

TIPS:In fact, you can see through the browser that the server uses the request forwarding method. When you click a hyperlink, the address of the current request will appear in the address bar of the browser, if the address in the address bar changes after the server responds, the request is forwarded indirectly. On the contrary, if the address does not change, it indicates Direct Request forwarding or no forwarding.

Q: What are the principles and differences between direct and indirect forwarding?

A: Forward and Redirect represent two request forwarding Methods: Direct forwarding and indirect forwarding. Corresponding to the code, they are the forward () method of the RequestDispatcher class and the sendRedirect () method of the HttpServletRequest class.

In indirect mode, when the server responds to the first request, the browser sends a request to another URL to forward the request. It is essentially two HTTP requests, corresponding to two request objects.

In direct mode, the client browser sends a request only once, and the Servlet forwards the request to Servlet, HTML, JSP, or other information resources. The request is responded by 2nd information resources, two information resources share the same request object.

Finally, I wish you all a satisfactory job!

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.