JSP Include Directive
JSP Include directive is used to copy the content of one JSP page to another. It's like including the code of one file into another.
<%@include file ="value"%>
Here value is the JSP file name which needs to be included. If the file is in the same directory then just specify the file name otherwise complete URL(or path) needs to be mentioned in the value field.
Note: It can be used anywhere in the page.
You can use the above code in your JSP page to copy the content of home.jsp file. However in this case both the JSP files must be in the same directory. If the home.jsp is in the different directory then instead of just file name you would need to specify the complete path in above code.
JSP include directive is used for merging external files to the current JSP page during translation phase (The phase where JSP gets converted into the equivalent Servlet).
Why we need to use the include directive?Can't we simply add the file's content in the current JSP instead of using the directive?
We can copy the content of external file and paste it in the main JSP, however it would not be a good practice. Let's understand this with the help of an example.I have 200 external files and 1 main JSP file. If I just copy the content of all files in the main JSP then I have to edit it whenever there is a change in any of the external file, instead we can include all files using directive and edit the particular file whenever needed.
Also, by using include directive you can enhance the code re-usability.Suppose there is a certain code or data which needs to be there in all the JSP pages of your application then you can simply have that code/data in one file and include the file in all the JSP pages.
Due to above this two reasons we require to use include directive.
Let's see an example for better clarity:
<%@ include file="home1.jsp" %>
Welcome to Silan Software
<%@ include file="home2.jsp" %>
Java means java8s.com and java8s.com means Java
Welcome to java8s.com
Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects
SILAN Technology, BBSR Call to: 0674-2361252