Difference betwixt include() as well as forward() methods of RequestDispatcher inward Servlert

What is the deviation betwixt include in addition to forrad methods of RequestDispatcher interface is ane of the frequently asked Servlet questions from Java EE interviews in addition to we'll encounter how you lot tin give the axe respond this query on your interview. You larn the RequestDispatcher reference either from ServletContext or ServletRequest interface in addition to fifty-fifty though both include() in addition to forward() method allow a Servlet to interact amongst only about other servlet, principal deviation betwixt include() in addition to forward is that include() method is used to charge the contents of the specified resources (could endure a Servlet, JSP, or static resources e.g. HTML files) straight into the Servlet's response, equally if it is business office of the calling Servlet. On the other hand, forward() method is used for server side redirection, where an HTTP asking for ane servlet is routed to only about other resources (Servlet, JSP file or HTML file) for processing.

This query is often asked equally a follow-up of to a greater extent than pop Servlet Question, the difference betwixt forward() in addition to sendRedirect() inward Servlet. Now, nosotros know the cardinal deviation betwixt these ii methods, let's encounter a distich of to a greater extent than differences to respond this query a niggling flake better.

Just retrieve that, fifty-fifty though you lot tin give the axe larn the RequestDispatcher reference from ServletContext.getRequestDispatcher() method or ServletRequest.getRequestDispatcher() method but in that place is a slight difference, the pathname of Servlet must laid about amongst a / and is interpreted equally relative to the electrical flow context root, acre inward example of ServletRequest, path tin give the axe endure relative.

Btw, if you lot are preparing for Java spider web or Java EE developer interview, you lot tin give the axe besides encounter the Java Programming Interview exposed for to a greater extent than questions on Servlet, JSP, Spring, in addition to Hibernate, key skills for Java spider web developers.




include() vs forrad inward Servlet

Before looking at the differences betwixt include() in addition to forward() method inward Servlet, let's encounter only about similarities betwixt them.

1) Both include() in addition to forward() methods are business office of RequestDispatcher interface of Servlet API

2) Both methods convey objects of ServletRequest in addition to ServletResponse interface.

3) Both include() in addition to forward() tin give the axe interact amongst static in addition to dynamic resources e.g. only about other Servlet, JSP or HTML files.


Now, let's encounter distich of differences betwixt include in addition to forward() method from Servlet API:

1) First in addition to initiatory deviation is that include() method includes the content of a resources inward the response, the resources could endure only about other Servlet, JSP or HTML file. While forward() method is used to forrad the asking to only about other resource.

2) The minute deviation betwixt include() in addition to forward() from Servlet API is that If you lot include a servlet or JSP document, the included resources must non endeavor to alter the response condition code or HTTP headers, whatsoever such asking volition endure ignored. On the other hand, If you lot include a Servlet or JSP document, the included resources must non endeavor to alter the response condition code or HTTP headers, whatsoever such asking volition endure ignored.

3) Third in addition to a useful deviation betwixt forward() in addition to include() method is that onetime is often used to include mutual boilerplate text of template markup which mightiness endure included past times many Servlets e.g. header or footer. While, forward() method is often used where a servlet is taking a controller role; processing only about input in addition to deciding the number past times returning a detail response page.

Here are ii diagrams which volition assistance you lot empathise how both include in addition to forrad methods of RequestDispatcher works inward Servlet application:

How include method of RequestDispatcher works
You tin give the axe encounter that the asking is for DispatcherServlet which calls include() method to include the response of Hello.jsp page, later on the response of DispatcherServlet in addition to Hello.jsp is combined in addition to sent to the client.

 What is the deviation betwixt include in addition to forrad methods of  Difference betwixt include() in addition to forward() methods of RequestDispatcher inward Servlert




How forrad method of RequestDispatcher works
You tin give the axe encounter the asking is for Servlet1 which forwards it to Servlet2, whose response is thus sent to the client. The response of Servlet1 is non used or non sent to the client.

 What is the deviation betwixt include in addition to forrad methods of  Difference betwixt include() in addition to forward() methods of RequestDispatcher inward Servlert


That's all most the difference betwixt include() in addition to forward() inward Servlet API. You should work include() method to charge a resources which could endure a JSP page or only about other Servlet, in addition to work forward() to redirect the asking to only about other resources for farther processing, over again you lot tin give the axe forrad the asking to only about other Servlet or JSP page.


Related Servlet in addition to JSP interview questions for Java programmers
  • Difference betwixt ServletContext in addition to ServletConfig inward Servlet? (answer)
  • Can you lot declare Constructor within Servlet class? (answer)
  • Difference betwixt GenericServlet in addition to HttpServlet inward Servlet API? (answer)
  • Difference betwixt the spider web server, application server in addition to Servlet container? (answer)
  • How to larn by customer session inward Servlet application? (answer)
  • Difference betwixt include directive in addition to include activity inward JSP? (answer)
  • Difference betwixt jsp:include in addition to jsp:forward activity inward JSP? (answer)


References
Java documentation of RequestDispatcher
Official Java EE vii tutorial from Oracle
Head First Servlet in addition to JSP

Subscribe to receive free email updates:

0 Response to "Difference betwixt include() as well as forward() methods of RequestDispatcher inward Servlert"

Posting Komentar