49

Forward() : This can be done in two ways by Request & ServeletContext. Forwarding a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. Forward is done at server side, without the client's knowledge.

When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completely with in the web container.

Simply

include: will include another file in our current file

forward: will forward the current request to the forwarding page

mtk
  • 11,504
  • 15
  • 67
  • 104
Ajay Takur
  • 5,287
  • 4
  • 31
  • 50
  • 16
    I did a Google search, and that's how I got here. Almost any question can be answered with "RTFM" or "STFW" but stackoverflow has become a fantastic resource for me because it offers succinct explanations and multiple perspectives in digestible sized chunks. I'm glad people ask questions that already have available answers. – WoodenKitty Oct 18 '14 at 04:51
  • 2
    Few things are more irritating than doing a Google search to find an answer and finding "Did you try something called Google search ?" – Arijit Sep 28 '17 at 06:46

4 Answers4

69

The difference between the two tags is that the way they work. Well, I will say an example so that you can imagine it better.

Assume you have two pages, pageA, and pageB. In pageA you wrote the include tag. In this case the control was in pageA til you called the include tag. At this point the full control goes to pageB. When It's done, control is returned to pageA starting from the next point of coding after the include tag and continuing to the rest of pageA.

Well, to get things much clearer, let's say that we have the same pages, pageA and pageB, but this time we will use the forward tag in pageA, not the include tag. Again, the control will begin in pageA till we call the forward tag in pageA, at this point, control is transfered to pageB, just like the include tag. But the difference is what happens when pageB completes. In case of the forward tag, control doesn't go back to pageA again.

oiyio
  • 3,108
  • 3
  • 34
  • 41
AmrAngry
  • 849
  • 1
  • 7
  • 4
  • 2
    Explanation was point on. Definitely needed it for a starter like myself. Accepted answer is correct but this example is more extended and complete than the accepted answer one. –  Feb 27 '17 at 05:50
35

The main difference is that when you use forward the control is transferred to the next servlet/jsp you are calling, while include retains the control with the current servlet, it just includes the processing done by the calling servlet/jsp(like doing any out.println or other processing).

mtk
  • 11,504
  • 15
  • 67
  • 104
  • To clarify the "control": With `include`, calling `response.sendError` or `response.sendRedirect` has **no effect** on the actual response. With `forward` it *does* manipulate the actual response. – Yeti Jan 23 '21 at 15:14
28

include(request, response);

If the resource is static, the include method enables programmatic server-side includes. If the resource is a web component, the effect of the method is to send the request to the included web component, execute the web component, and then include the result of the execution in the response from the containing servlet.

An included web component has access to the request object but is limited in what it can do with the response object.

  • It can write to the body of the response and commit a response.
  • It cannot set headers or call any method, such as setCookie, that affects the headers of the response.

It is often useful to include another web resource, such as banner content or copyright information) in the response returned from a web component.

forward(request, response);

In some applications, you might want to have one web component do preliminary processing of a request and have another component generate the response. For example, you might want to partially process a request and then transfer to another component, depending on the nature of the request.

To transfer control to another web component, you invoke the forward method of a RequestDispatcher. When a request is forwarded, the request URL is set to the path of the forwarded page. The original URI and its constituent parts are saved as request attributes.

javax.servlet.forward.[request-uri|context-path|servlet-path|path-info|query-string]  

The forward method should be used to give another resource responsibility for replying to the user. If you have already accessed a ServletOutputStream or PrintWriter object within the servlet, you cannot use this method; doing so throws an IllegalStateException.

Related links

Community
  • 1
  • 1
Aniket Kulkarni
  • 12,142
  • 9
  • 65
  • 83
9

The key difference between the two is that the forward() method will CLOSE the output stream after it has been invoked, whereas the include method leaves the output stream OPEN.

answering with an example : lets have a servlet page named xxx.java and a jsp page named yy.jsp

In the yy.jsp

WELCOME to yy.jsp

In the xxx.java //using forward()

RequestDispatcher rd = request.getRequestDispatcher("yy.jsp"); rd.forward(request,response); out.println("back to servlet"); //this wont b displayed

output

WELCOME to yy.jsp 

In the xxx.java //using include()

RequestDispatcher rd = request.getRequestDispatcher("yy.jsp"); rd.include(request,response); out.println("back to servlet");

output

WELCOME to yy.jsp back to servlet

BUT MOST IMPORTANTLY ITS NOT ABOUT THE CONTROL, BECAUSE IF WE PUT a

System.out.println("console output");

after either of the .forward() or .include() invocation, the console output will get generated on each case.Its about the response to the client

So, the basic part is if we are processing in a server side component and then forward to a JSP or Servlet in order to generate markup for a client, once that JSP or Servlet has finished processing, we can no longer call on any other components to generate markup that can be sent to the client. Once we have performed a forward, markup generation for the current request and response cycle is finished.

Alternatively, with an include, the output stream remains open, so we can call on as many different files to generate client side markup that we need. So we can include two or three JSP files and even a Servlet in the chain of components that generate client based markup. When we use an include, the output stream is not closed after invocation.

uttsav
  • 139
  • 1
  • 3