Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using variable page name in <jsp:include...

Status
Not open for further replies.

jgreene78023

Programmer
Jun 23, 2003
3
US
I'm trying to do a jsp include of a page, the url of which will be determined at runtime, but it doesn't work. Here's an example of what I'm trying to do in my jsp:

<%
String myFilename = &quot;someUrl.jsp&quot;; (determined at runtime)
%>
.
.
.
<body>
.
.
.
<jsp:include page=&quot;<%= myFilename %>&quot; flush=&quot;true&quot; />
.
.
.

This will not work for me. I've also tried using the include directive syntax, but of course this didn't work either. I did a search on this forum's archives, and found a thread where someone said this could be done exactly as I've tried. Can anyone offer any updated information as to why I cannot do this, or suggestions as to what I might be doing wrong? Thanks so much for any help.

- Joe
 
Joe,

The include tags are handled by the JSP engine. If you know what the JSP engine does you should understand why this does not work. All the JSP engine does is generate a Servlet java class and compile it. The compiled Servlet is what is executed at runtime.

-pete
 
Pete,
Thanks for your reply...

Perhaps there's a semantic issue here. Maybe I should have said something like 'request time' or 'execution time' instead of 'runtime'. My point is that when my main jsp is being rebuilt and recompiled by the servlet engine at request time, the dynamic variable used in the <jsp:include... /> tag doesn't work. I have an object riding with the session that knows what filename should be inserted between the <%=..%> tags. Yet when the jsp is being recompiled, the best I'm able to have happen is that the filename string is inserted between the <%= ... %> tags, and the entire literal string &quot;<jsp:include page=&quot;somefilename&quot; flush=&quot;true&quot; />&quot; appears in my html output. The contents of the file I'm trying to include are not included.

If you do a keyword search on this forum for &quot;jsp variable includes&quot;, you'll see the thread where the author claims this should work. If there's still something I'm not getting, sorry about the confusion. I've done a lot of jsps, but have never tried to do a conditional include using a variable inside the include tags. I've used this same approach with the <jsp:forward.../> tags, which works fine, but there seems to be something goofy about trying to include output from a dynamically determined jsp.

Thanks again for any help.

- Joe
 
Sorry Joe, i misread your original post. Ignore my reply. That technique works for me without any problems. I can't imagine what might be wrong in your case.

Are you executing it in a JSP page? Do you have any other JSP code executing in that page?

-pete
 
Pete -
Thanks again for your reply. Yes, the top of the page has a few lines of java code that executes, and if you look at my original post, that is a very accurate example of what I'm trying to do. As a test, I created the String variable at the top of the jsp so I could try to use the <%=...%> tags inside the <jsp:include... /> tag. It won't seem to work, though. If you are able to do this without a problem, maybe I've just got a cockpit error here. I'll keep looking. Thanks again.

- Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top