Difference between include and forward mechanism for request dispatching concept?

牧云@^-^@ 提交于 2019-12-17 10:16:13

问题


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


回答1:


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).




回答2:


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.




回答3:


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

  • RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
  • RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
  • Java EE 6 Tutorial



回答4:


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.



来源:https://stackoverflow.com/questions/9432912/difference-between-include-and-forward-mechanism-for-request-dispatching-concept

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!