Problem with <jsp:include on Tomcat 5

G

Grudak

Hi all, I hope someone can help me understand why this doesn't work. I
guess I don't understand how <jsp:include is supposed to work... :)

here is include.jsp:
<html>
<body>
<jsp:include page="include.jspf" />
</body>
</html>

and here is include.jspf:
<% String a = "hello"; %>
<%=a%>

when I browse to include.jsp, I would expect to see the word hello in
my browser. However, there is nothing in the browser. when I look at
the source, I see:
<html>
<body>
<% String a = "hello"; %>
<%=a%>
</body>
</html>

what gives??? it seems to be blindly including the jspf file into the
source, rather than *evaluating* the .jsfp file. If I paste the
contents of the .jspf into the .jsp file in place of the <jsp:include
statement, I get the word hello, but that defeats the purpose of code
reuse.

Help???
 
A

Andrea Desole

Hi all, I hope someone can help me understand why this doesn't work. I
guess I don't understand how <jsp:include is supposed to work... :)

You mostly do.
What happens is that jsp:include include the page dynamically, that is
when the page is requested. In this case your included page,
include.jspf, is completely separated from your first page, include.jsp.
Because of this the server has to compile both pages. Now, what I
believe is that your server doesn't know how to compile include.jspf
because of its extension. If you configure Tomcat to compile jspf pages
(you can do that in the web.xml of Tomcat), or if you rename the page to
jsp, it will probably work as expected.
Now, although changing the Tomcat configuration can be a solution, think
about it. What you are using is a jspf, which means "JSP fragment". A
jspf is not meant to be used on its own, at the point that it's usually
put in the WEB-INF directory, where a client can't access it. Because of
this, it doesn't usually make sense to include a jspf dynamically. So,
the possible solutions are probably:

1) rename the jspf to a jsp
2) use the static include, that is, <%@ include ... %>
 
G

Grudak

Thanks for the info. Sure enough, just changing the name of the frag
file to .jsp fixed the issue.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top