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 adds the 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 the 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 an 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.
The third option to include the output of one JSP page into another is the 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 resources 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 the import tag. You can also see these free Servlet and JSP courses to learn more about essential JSP concepts like these.
include directive
JSP include action
and JSTL import tag
The include directive provides static inclusion. It adds the 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 the 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 an 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.
The third option to include the output of one JSP page into another is the 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 resources 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 the import tag. You can also see these free Servlet and JSP courses to learn more about essential JSP concepts like these.
Include Directive vs Include Action vs JSTL Import
In summary, here are the main difference between include directive, include action, and JSTL import tag in JSP:1. The main difference between include directive and include action is that the former is static include while the latter is dynamic include. Also former uses a file attribute to specify the location of the resource to be include while later uses the page attribute.
2. The key difference between include action and JSTL import tag is that the 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.
Related Servlet and JSP interview questions for Java programmers
- 20+ Spring and REST Interview Questions with Answers (questions)
- 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 sessions in a Servlet application? (answer)
- Difference between include directive and include action in JSP? (answer)
- Difference between jsp:include and jsp:forward action in JSP? (answer)
- 5 Courses to learn Servlet and JSP for free (courses)
- 20+ Spring MVC Interview Questions for Java Developers (questions)
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.