What is the difference between include and forward methods of RequestDispatcher interface is one of the frequently asked Servlet questions from Java EE interviews and we'll see how you can answer this question on your interview. You get the RequestDispatcher reference either from ServletContext or ServletRequest interface and even though both include() and forward() method allow a Servlet to interact with another servlet, the main difference between include() and forward is that the include() method is used to load the contents of the specified resource (could be a Servlet, JSP, or static resource like HTML files) directly into the Servlet's response, as if it is part of the calling Servlet.
On the other hand, forward() method is used for server side redirection, where an HTTP request for one servlet is routed to another resource (Servlet, JSP file or HTML file) for processing.
This question is often asked as a follow-up of more popular Servlet Question, the difference between forward() and sendRedirect() in Servlet. Now, we know the fundamental difference between these two methods, let's see a couple of more differences to answer this question a little bit better.
Just remember that, even though you can get the RequestDispatcher reference from ServletContext.getRequestDispatcher() method or ServletRequest.getRequestDispatcher() method but there is a slight difference, the pathname of Servlet must begin with a / and is interpreted as relative to the current context root, while in case of ServletRequest, path can be relative.
Btw, if you are preparing for Java web or Java EE developer interview, you can also see the Java Programming Interview exposed for more questions on Servlet, JSP, Spring, and Hibernate, key skills for Java web developers.
1) Both include() and forward() methods are part of RequestDispatcher interface of Servlet API
2) Both methods accept objects of ServletRequest and ServletResponse interface.
3) Both include() and forward() can interact with static and dynamic resource e.g. another Servlet, JSP or HTML files.
Now, let's see couple of differences between include and forward() method from Servlet API:
1. First and foremost difference is that the include() method includes the content of a resource in the response, the resource could be another Servlet, JSP or HTML file. While the forward() method is used to forward the request to another resource.
2. The second difference between include() and forward() from Servlet API is that If you include a servlet or JSP document, the included resource must not attempt to change the response status code or HTTP headers, any such request will be ignored.
This question is often asked as a follow-up of more popular Servlet Question, the difference between forward() and sendRedirect() in Servlet. Now, we know the fundamental difference between these two methods, let's see a couple of more differences to answer this question a little bit better.
Just remember that, even though you can get the RequestDispatcher reference from ServletContext.getRequestDispatcher() method or ServletRequest.getRequestDispatcher() method but there is a slight difference, the pathname of Servlet must begin with a / and is interpreted as relative to the current context root, while in case of ServletRequest, path can be relative.
Btw, if you are preparing for Java web or Java EE developer interview, you can also see the Java Programming Interview exposed for more questions on Servlet, JSP, Spring, and Hibernate, key skills for Java web developers.
include() vs forward in Servlet
Before looking at the differences between include() and forward() method in Servlet, let's see some similarities between them.1) Both include() and forward() methods are part of RequestDispatcher interface of Servlet API
2) Both methods accept objects of ServletRequest and ServletResponse interface.
3) Both include() and forward() can interact with static and dynamic resource e.g. another Servlet, JSP or HTML files.
Now, let's see couple of differences between include and forward() method from Servlet API:
1. First and foremost difference is that the include() method includes the content of a resource in the response, the resource could be another Servlet, JSP or HTML file. While the forward() method is used to forward the request to another resource.
2. The second difference between include() and forward() from Servlet API is that If you include a servlet or JSP document, the included resource must not attempt to change the response status code or HTTP headers, any such request will be ignored.
On the other hand, If you include a Servlet or JSP document, the included resource must not attempt to change the response status code or HTTP headers, any such request will be ignored.
3. Third and the useful difference between the forward() and include() method is that the former is often used to include common boilerplate text of template markup which might be included by many Servlets e.g. header or footer. While, forward() method is often used where a servlet is taking a controller role; processing some input, and deciding the outcome by returning a particular response page.
Here are two diagrams which will help you understand how both include and forward methods of RequestDispatcher works in Servlet application:
3. Third and the useful difference between the forward() and include() method is that the former is often used to include common boilerplate text of template markup which might be included by many Servlets e.g. header or footer. While, forward() method is often used where a servlet is taking a controller role; processing some input, and deciding the outcome by returning a particular response page.
Here are two diagrams which will help you understand how both include and forward methods of RequestDispatcher works in Servlet application:
How include method of RequestDispatcher works
You can see that the request is for DispatcherServlet which calls include() method to include the response of Hello.jsp page, later the response of DispatcherServlet and Hello.jsp is combined and sent to the client.How forward method of RequestDispatcher works
You can see the request is for Servlet1 which forwards it to Servlet2, whose response is then sent to the client. The response of Servlet is not used or not sent to the client.That's all about the difference between include() and forward() in Servlet API. You should use include() method to load a resource which could be a JSP page or another Servlet, and use forward() to redirect the request to another resource for further processing, again you can forward the request to another Servlet or JSP page.
Related Servlet and JSP interview questions for Java programmers
- Difference between ServletContext and ServletConfig in Servlet? (answer)
- Can you declare Constructor inside Servlet class? (answer)
- Difference between GenericServlet and HttpServlet in Servlet API? (answer)
- Difference between the web server, application server and Servlet container? (answer)
- How to manage client session in Servlet application? (answer)
- Difference between include directive and include action in JSP? (answer)
- Difference between jsp:include and jsp:forward action in JSP? (answer)
2) The second difference between include() and forward() from Servlet API is that If you include a servlet or JSP document, the included resource must not attempt to change the response status code or HTTP headers, any such request will be ignored. On the other hand, If you include a Servlet or JSP document, the included resource must not attempt to change the response status code or HTTP headers, any such request will be ignored.
ReplyDelete2nd differece is not correctly explaind..
Thank you soo much!:) It was exactly what I was looking for.
ReplyDelete