Categories
Java

JSP and dynamic page includes

Coming from the warm sticky confines of ColdFusion development, one gets used to a nifty – and at the time – natural – way of including code dynamically within one’s application. What I am talking about is the ability to tell the server to include a file using a variable name. The server then reads the file, interprets and executes its contents and you separate as friends. In ColdFusion, it looks like this:

<cfinclude url="#var_with_name_of_file_to_include_and_run#">

JSPs have three facitilities to try and do the same thing:
<%@include ... > allows you to include a file statically and have it run. This means that when the JSP is loaded for the first time, and is compiled into a servlet, the include is dropped into the JSP prior to compilation and is compiled into the resulting servlet. If you change the included file, the change will not be reflected. The problem with this otherwise great option is that it cannot get the name of the included file dynamically, as in a variable, as it needs to know the name of the file to include and compile.

Facility number two: <jsp:include ... > does a similar thing: it includes the file into the JSP code but does so every time from scratch, when the page is loaded. If the included file is chagned, the change will be reflected. You can even specify the name of the file dynamically. Problem here is that the included file, even if it is a JSP, will not be executed or parsed and will appear in source form inside the enclosing JSP. This sucks.

The third facility addresses a deficiency that apparently really bothered someone: <jsp:include ... > does not allow you to include files from other contexts, but only files that can be referred to relatively. So when JSTL came into being, its core library’s import tag allows that access to outside-of-context files. But neither can this newer tag offer the dynamic inclusion and execution of files.

The outcome, we found a different path to rendering our pages dynamically, where instead of putting the dynamic content inside the navigation and graphics, we put the navigation into the file we intended to include. Getting there, though, was frustrating and this feels like somewhat of a glaring shortcoming of JSP.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Share