How to Include a File in a JSP
- 1). Select the file to include and the JSP that it will be included into. The file to include does not necessarily need to be a JSP, but you will need to include it into a JSP because the include statements only work in JSP files.
- 2). Determine the location of both files in the web application. You will need to know these locations since you will need to include the relative path of the file to include in the include statement.
- 3). Select the method of inclusion. There are two different ways to define an include in a JSP, and they each have slightly different ramifications.
The include directive statement copies the contents of the included file into the main JSP when the main JSP is translated into Java code (specifically a Java Servlet). Because of this, it is preferable to use this method when the content of the included file won't change often.
The include action statement executes the included file independently at runtime and merges the results of that execution into the main JSP. This is the better method when you expect the content of the included file to change each time it is called.
- 4). Add code to the main JSP for the chosen method of inclusion. To use the include directive, you will add this code to your JSP file:
<%@ include file="included/FileToInclude.jsp" %>
You will then change "included/FileToInclude.jsp" to the relative path and name of the file you wish to include.
To use the include action, add this code to your JSP file:
As with the include directive statement, you will change "included/FileToInclude.jsp" to the name of the file you want to include.
- 5). Deploy both the file to include and the main JSP. The file to include should be included the next time the main JSP is executed.