Difference between include directive, include action and JSTL import tag in JSP?

There are three main ways to include the content of one JSP into another:

include directive

JSP include action

and JSTL import tag

The include directive provides static inclusion. It ads content of the resource specified by its file attribute, which could be HTML, JSP or any other resource at translation time.

Any change you make in the file to be included after JSP is translated will not be picked up by include directive.


Since JSP is translated only once but can be requested many times it's not a very useful option. It was originally intended to include static contents like HTML header and footer for a web page.

The main difference between include directive and include action is that JSP includes action provides dynamic inclusion.

The content of another JSP or HTML page is included at request time, which any change you make in the file to be included will be visible to another JSP when requested.

This is ideal if you are importing content from dynamic resources like another JSP page. The file to be included is specified by page attribute of jsp: include tag.

Third option to include the output of one JSP page into another is JSTL import tag. This works pretty much like include action but the main difference between import tag and includes action is that import tag can include resources from the outside world.


If you use include directive or include action you are only limited to include the output of resource from the same project or another web application residing in the same container.

It is the most powerful and flexible way to include the content of one JSP to another. The file to be included is specified by url attribute of import tag.

Difference between include directive, include action and JSTL import tag in JSP?


In short

1) The main difference between include directive and include action is that the former is static include while later is dynamic include. Also former uses file attribute to specify the location of the resource to be include while later uses page attribute.


3) The key difference between include action and JSTL import tag is that former can only include local resources but later can also include the output of remote resources, JSP pages or HTML pages outside the web container. include action uses page attribute while import tag uses URL attribute.


That's all on the difference between include direction, include action and JSTL import tag in JSP. It's also an important JSP Interview question and understanding these concepts will help you to do well in your next interview.



Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets, and JDBC for Beginners: Build a Database App


Related Servlet and JSP interview questions for Java programmers
  1. 20+ Spring and REST Interview Questions with Answers (questions
  2. Difference between ServletContext and ServletConfig in Servlet? (answer
  3. Can you declare Constructor inside Servlet class? (answer
  4. Difference between GenericServlet and HttpServlet in Servlet API? (answer
  5. Difference between the web server, application server and Servlet container? (answer
  6. How to manage client session in a Servlet application? (answer
  7. Difference between include directive and include action in JSP? (answer
  8. Difference between jsp:include and jsp:forward action in JSP? (answer
  9. 5 Courses to learn Servlet and JSP for free (courses
  10. 20+ Spring MVC Interview Questions for Java Developers (questions

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

No comments:

Post a Comment