FAQ


Advertisement



When and How to use inclusion in JSP pages?

The purpose of using inclusion in JSP pages is to reduce code redunancy and to promote reusability.

In many web applications, there are some common elements you need to display in many pages. For example, comany logo, copy right information, and so on. Instead of reproducing those common pieces in each of JSP pages, you can factor those common code into other files and include them into JSP pages using inclusion mechanism provided by JSP engine. The application's maintainability (you just need to make changes in only one palce) and reusability gets improved.

JSP engine supports two include mechanisms: include directive and include action (or jsp:include).

Include Directive

The syntax of include directive is as follow:

<%@ include file="relativeURL" %>

The JSP's include directive inserts the content of a specified file in calling JSP file at translation time, when calling JSP file is compiled. The included file can be a JSP file, HTML file, or a plain text file. If the included file is a JSP file, its JSP elements are translated and included (along with any other text) in the calling JSP file. Once the included file is translated and included, the translation process resumes with the next line of the calling JSP file.

When you prepare included file for include directive, make sure it does not contain , , , and tags. Because the entire content of the included file is added to the calling JSP file, these tags would conflict with the same tags in the calling JSP file, causing an error. Since you include the content of any file specified by the include directive in the calling JSP before it compiles, variables and other values specified in the calling JSP can also be accessed in the included content. Even though, it is the good practice to avoid it to make included file more reusable.

Usally when you want to include static content, like page header and footer, you can use include directive.

Include Action (or jsp:include)

The syntax of include action is as follow:

<jsp:include page="{relativeURL | <%= expression %>}" />
or
<jsp:include page="{relativeURL | <%= expression %>}">
<jsp:param name="parameterName"
value="{parameterValue | <%= expression %>}" />
</jsp:include>

The JSP's include action (or jsp:include) allows you to include either a static or dynamic file in a JSP file. The results of including static and dynamic files are quite different. If the file is static, its content is included in the calling JSP file. If the file is dynamic, it acts on a request and then places the generated response in the calling JSP. When the include action is finished, the JSP container continues processing the remainder of the JSP file.

Usally when you cannot determine whether the file is static or dynamic, you can use include action. Notice that you cannot determine from the file name if it is static or dynamic. For instance, http://service/index.html may map to a dynamic servlet through a web server alias.

Include Directive vs. Include Action

The include directive allows a static resource to be included in the page at translation time (JSP compile time), and it is then parsed/translated along with the rest of the JSP. The resulting page as one page therefore creating one corresponding servlet. It is the equivalent to cutting and pasting the text from your include page right into your JSP.

The include action on the other hand is a request time include, the included file is treated a separate servlet which is executed at request time and its output "chained" to the including servlet. The output produced by that (second) JSP is included without being parsed or translated by the first JSP.

Another way to think of it is the include directive version is a static include -- it is a partial JSP page that normally is not run on its own (maybe a header or footer that you use on all your pages). The include file is done at translation-time. The include directive should be used to include static content.

The include action version is a dynamic include it is a full JSP that is executed, and its output is included in the parent JSP. The include page is done at request-time. The include action should be used to include static/dynamic content.

So we expect static content not to be updated when using the include directive. But as you have experienced, content is actually updated, even with the include directive. Why?

The specification also says about the include directive:A JSP container can include a mechanism for being notified if an included file changes, so the container can recompile the JSP page. However, the JSP 2.0 specification does not have a way of directing the JSP container that included files have changed. It is up to the container to do whatever it wants. There maybe containers that will update pages using the include directive. The container you are using is acting this way. It will update the content of an included file, even when using the include directive. But the specification insists that the directive should be used for static content only, and that you should not rely on the container's behaviour.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |