Objective 7) The include directive and standard action

6.7) Given a specific design goal for including a JSP segment in another page, write the JSP code that uses the most appropriate inclusion mechanism (the include directive or the jsp:include standard action).

The two includes are similar

Both forms of include, the include directive and the include standard action allow you to automatically include the content of one file inside another. This can be useful when you have the same set of text that should appear in multiple files. Rather than manually copying and pasting the duplicated code you can use both includes to do it automatically. This brings the benefit that if you need to change the duplicated code you only have to do it once.

The includes have important differences

The include directive and the JSP:include standard action look alike and much of the time the results are the same. However they differ in that the include directive ( <%@ include ) happens at translation time and the include action (<jsp:include ) happens at runtime. The include directive is effectively the same as copying and pasting text into the page, except the container does it for you automagically.

The include standard (<jsp:include ) action inserts the included text into the response at runtime. This means it brings with it the advantage that you can be certain that the output always contains the “latest” version of the included text. By contrast with the include directive ( <%@ include ) the included text is copied in the first time the page is accessed (when the JSP is translated into a servlet).

If you subsequently change the included file you have no guarantee that the jsp will be re-compiled and thus you may end up with an “old” version of the included text. Note that I said you have no guarantee, some containers will monitor files and force a re-translation and compilation when a file changes, however the JSP specification does not guarantee this so you are advised not to rely on it.

Variable visibility and the includes

Because the include standard action (<jsp:include inserts the included text into the response at runtime, the included file will not have access to variables created in the including file. Thus the following code.will run as expected and will output the string Marcus in the included file.

<%! String myname="marcus"; %>
<%@include file="menu.jsp" %>

<h1>Contents of menu.jsp </h1>

By contrast if you replace the include directive with include standard action thus

<%! String myname="marcus"; %>
<jsp:include page="menu.jsp" />

A compilation error will be thrown, which on my Tomcat 5 installation was as follows.

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 2 in the jsp file: /menu.jsp
Generated servlet error:
C:\Documents and Settings\marcus\.netbeans\5.0\jakarta-tomcat
a:44: cannot find symbol symbol : variable myname location: class org.apache.jsp.menu_jsp out.print(myname);

The syntax of the includes

As if to make life harder for the JSP programmer the two includes have a different keyword to refer to the file. For the directive it is the word file, i.e.

<%@ include file=”file.jsp” %>

Whereas for the action it is the word page

<jsp:include page =”file.jsp” />

Adding parameters to the include action

<jsp:include page="menu.jsp"> 
    <jsp:param name="username" value = "Fred" />

The included file shows how you can retrieve the parameters either using EL or the implicit request object in a scriptlet.

Username is ${param.username}
Username is <%=request.getParameter("username") %>

Other Sources

The include standard action according to Sun

The include directive according to Sun

According to Mikalai Zaikin

Last modified: Sunday, 20 September 2015, 07:21 PM