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 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 difference 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 e.g. composing JSP page with header portion coming form 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 difference between include directive and include action in JSP:

1) The most cirtical 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 thtat an include directive can use both relative and absolute path but the include action always uses relative path.

3) Third key diference 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 the 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 configuraiton, but with include action, we can pass another parameter also using <jsp:param> tags.

5) Fifth difference is extension of previous difference i.e. with 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 the least difference between include directive and include action is that 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 directive 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 Interview. 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.

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 method in HTTP Protocol? (answer)


Further Reading



No comments:

Post a Comment