Dynamically include JSP file?

  • Thread starter Joona I Palaste
  • Start date
J

Joona I Palaste

Can I somehow dynamically decide which JSP file to include in another
JSP file, using the <%@include%> tag? Specifically, what I want to do is
something like this:

In the main JSP file:
String jspFile = getJSPFileName();
<%@include file= jspFile %>

So that the value of the variable jspFile is the name of the JSP file
being included in the main JSP file. I don't want to use a chain of
if (jspFile.equals("...")) statements, as that would defeat my whole
purpose.
Thanks for your help!
 
M

Michael Borgwardt

Joona said:
Can I somehow dynamically decide which JSP file to include in another
JSP file, using the <%@include%> tag?

No, because the include directive effectively copies the included
page's text into your JSP before it's even compiled!

Instead, you have to use an include action:

<jsp:include page='<%= jspFile %>' />
 
T

Tor Iver Wilhelmsen

Michael Borgwardt said:
No, because the include directive effectively copies the included
page's text into your JSP before it's even compiled!

Yes, but you can surround <%@include %> directives with if/else; but
the result would be one huge servlet, because of the inlining ou
mention. Dynamic includes as you mentioned are better.
 
J

Joona I Palaste

No, because the include directive effectively copies the included
page's text into your JSP before it's even compiled!
Instead, you have to use an include action:
<jsp:include page='<%= jspFile %>' />

This doesn't work at all. I tried the following JSP code:

// Decide which kind of document to render (html/printable/pdf)
String jspFile = Web.getInstance().getJSP(request);
<jsp:include page='<%= jspFile %>' flush="true"/>

and got this Apache Tomcat error:

An error occurred between lines: 1 and 4 in the jsp file: /ok.jsp

Generated servlet error:
/tmp/ok$jsp.java:63: Missing term.
String jspFile =
Web.getInstance().getJSP(request);
^


An error occurred between lines: 1 and 4 in the jsp file: /ok.jsp

Generated servlet error:
/tmp/ok$jsp.java:64: Invalid character constant.
<jsp:include page='<%= jspFile
^


An error occurred between lines: 1 and 4 in the jsp file: /ok.jsp

Generated servlet error:
/tmp/ok$jsp.java:64: ';' expected.
<jsp:include page='<%= jspFile
^

The relevant part of the ok$jsp.java source code is as follows:

// Decide which kind of document to render
(html/printable/pdf)
String jspFile = Web.getInstance().getJSP(request);
<jsp:include page='<%= jspFile
// end
// HTML // begin [file="/ok.jsp";from=(3,35);to=(5,0)]
out.write("' flush=\"true\"/>\n%>\n");

// end

It appears that the JSP compiler does not understand the <jsp:include>
tag at all. Is it possible that it is not supported in Apache Tomcat
4.0.6? Do I have to use a big chain of if/else statements now?
 
J

Joona I Palaste

This doesn't work at all. I tried the following JSP code:
// Decide which kind of document to render (html/printable/pdf)
String jspFile = Web.getInstance().getJSP(request);
<jsp:include page='<%= jspFile %>' flush="true"/>
and got this Apache Tomcat error:

Dang, now I realise my mistake. I've been trying to use the
<jsp:include> action as part of embedded Java code, not as JSP code.
D'oh!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top