using a variable within a JSP include directive

F

Falk Köppe

Hello,

I have a problem using a variable within a JSP include directive. The
following code does not work for me:

<%
String page = "examplepage.jsp";
%>

<@ include file="<%= page %>" %>
or
<@ include file="${page}" %>
or
<@ include file=page %>

Is it possible to use a variable within a JSP include directive? I
know that I could use a variable within a JSP include action
(<jsp:include page="<%= page %>" /> or <jsp:include page="${page}" />)
but the content of my page does not change very often, so it should be
included at compile time.

Cheers
Falk Köppe
 
J

John B. Matthews

Falk Köppe said:
Hello,

I have a problem using a variable within a JSP include directive. The
following code does not work for me:

<%
String page = "examplepage.jsp";
%>

<@ include file="<%= page %>" %>
or
<@ include file="${page}" %>
or
<@ include file=page %>

Is it possible to use a variable within a JSP include directive?

No, the value of the variable "page" is established when the compiled
servlet is executed. In contrast, the include directive is applied at
the time the JSP page is translated into a servlet, before the String
containing the page name exits.
I know that I could use a variable within a JSP include action
(<jsp:include page="<%= page %>" /> or <jsp:include page="${page}"
/>) but the content of my page does not change very often, so it
should be included at compile time.

Out of curiosity, why not just use the include directive:

<%@ include file="examplepage.jsp" %>

[Note corrected JSP directive syntax.]
 
F

Falk Köppe

Thank you for your answer.
No, the value of the variable "page" is established when the compiled
servlet is executed. In contrast, the include directive is applied at
the time the JSP page is translated into a servlet, before the String
containing the page name exits.

You are totally right that the variable is first established, after
the include directive is evaluated. I did not think about this.
Out of curiosity, why not just use the include directive:

<%@ include file="examplepage.jsp" %>

Actually my specific example was, that I tried to introduce a include-
folder variable, so I can easily switch to another directory if I have
to.

<%@ include file=${include_location}examplepage.jsp %>

If this include-folder variable would be used in all include variables
I could easily rename or move the folder without changing all include
directives. But you are again absolutely right, that I will now only
work with relative paths, leaving the name and the relative location
of the include folder intact.

Thank you again for pointing me to the sequence of statement
evaluation, which I just not thought of.

Greetings
Falk Köppe
 
M

Mark Space

Falk said:
<%@ include file=${include_location}examplepage.jsp %>

If this include-folder variable would be used in all include variables
I could easily rename or move the folder without changing all include
directives. But you are again absolutely right, that I will now only
work with relative paths, leaving the name and the relative location
of the include folder intact.

One thing you can do is treat your include_location like a object file
and have it built at during the compile/deploy step.

Given:
include_a/example_stuff.jsp
include_b/example_stuff.jsp

Have your build script decide which file to copy to the eventual location:

include_location/example_stuff.jsp // determined at build time
// from _a or _b
 
W

Wojtek

Mark Space wrote :
One thing you can do is treat your include_location like a object file and
have it built at during the compile/deploy step.

Given:
include_a/example_stuff.jsp
include_b/example_stuff.jsp

Have your build script decide which file to copy to the eventual location:

include_location/example_stuff.jsp // determined at build time
// from _a or _b

Which would still not allow a different file to included during
runtime, only compile time.

I'll bet the OP is coming from PHP which is interpreted and therefore
you can include different files during runtime.

jsp OTOH is compiled once, then the resulting class is cached and
reused many times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top