6 Difference between include directive and include action in JSP

Difference between include directive and include action
Even though both include directive and include action allows you to include the response of one JSP into another, they are quite different from each other e.g. include directive is processed at translation time and used to include the static resources e.g. HTML files, images, and CSS, etc, while the <jsp:include> action is processed at request time and used to include more dynamic resources e.g. JSP or Servlet. What is the difference between include directive and include action is also one of the most popular JSP interview questions, mostly asked either at telephonic round or first few round of Java web developer interviews?

Let's see a couple of more differences between these two to answer this question in more detail. Btw, if you are new in JSP and just learning and preparing for interviews at the same time, then you should also look at Head First Servlet and JSP, one of the best books to learn and prepare for Servlet JSP interviews and certification at the same time.



JSP include directive vs include action

The include directive is one the three directive supported by JSP e.g. @taglib, @include and @page, while the include action is one of the many standard actions supported by JSP i.e. <jsp:include>, <jsp:forward>, <jsp:useBean>, <jsp:setProperty>, <jsp:getProperty>, <jsp:param> and <jsp:plugin>.

Both are used to include the response of one resource into another like composing a JSP page with a header portion coming from the header.jsp, footer coming from footer.jsp and sidebar coming from sidebar.jsp. But as I said, they are very different from each other.

Here are some important differences between include directive and include action in JSP:

1. The most critical difference between @include and <jsp:include> is that the include directive is processed at the translation time but include action i.e. <jsp:include> is processed at the request time i.e. when the request comes for processing.

2. Second important difference between include directive and include action is that an include directive can use both relative and absolute paths but the include action always uses a relative path.

3. Third key difference between include action and include directive is that @include can only include contents of resource e.g. HTML or CSS file but it will not process the dynamic resource, while they include action i.e. <jsp:include> process the dynamic resource and result will be added to calling JSP.

4. One more practical difference between include directive and include action is that with include directive, you can not pass any other parameter e.g request, response, or any configuration, but with include action, we can pass another parameter also using <jsp:param> tags.

5. The Fifth difference is an extension of the previous difference i.e. with the include directive, you cannot pass the request or response object to calling jsp to included file or JSP or vice versa, but with include action, it's possible to pass request/response object.

6. Last but not least difference between include directive and include action is that the former uses the file attribute to specify the resource to be included and that's why know as file include as well. On the other hand include action uses page attribute to specify the resource to be included, hence also known as page include in JSP.

Here is a nice summary of all the differences between file include and page include i.e. include action in JSP:

Difference between include directive and include action in JSP



That's all about the difference between include directives and include action in JSP. As I said, it's one of the classical questions from Servlet and JSP interviews and frequently asked during the telephonic round of Interviews.

Related JSP and Servlet Interview Questions
  • Difference between Web Server and Application Server? (answer)
  • How to upload a File in JSP and Servlet? (answer)
  • Difference between URL rewriting and URL encoding in JSP? (answer)
  • What is JSESSIONID in J2EE Web Application? (answer)
  • Difference between sendRedirect() and forward() in Servlet? (answer)
  • Difference between GET and POST methods in HTTP Protocol? (answer)

While answering this question you should remember to mention the key points like include action does request time inclusion and you can include dynamic resources like response of another JSP or Servlet using include action

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.