Forums Register Login

include (directive x action)

+Pie Number of slices to send: Send
Hi all,

I learned that <%@ include file="file" %> and "<jsp:include page="file" /> behaved differently in the sense that the first includes the source of the target page at translation/compile time and the second performs a request and includes the result in runtime.

So I tried to test this in practice and the results did not confirm expectations. First I created a simple jsp file to print the time. Then I created another jsp including the first one twice, like this:

<h1>Directive</h1>
<%@ include file="chronos.jsp" %>
<hr>
<h1>Action</h1>
<jsp:include page="chronos.jsp" />

I imagined that I would have 2 different results, but they turn out to be exactly the same, that is, the directive is also refreshing its time at each request.

What am I missing?

Cesar Moura
+Pie Number of slices to send: Send
The directive <%@ include file="file" %> merges the code at compile time.

The include <jsp:include page="file" /> executes the included page at run time and merges the result.

They would both print the current time.





[stupid smilies]
[ April 22, 2005: Message edited by: Ben Souther ]
+Pie Number of slices to send: Send
How are you "printing the time"?

If it's a call along the lines of:



That is going to execute at run-time regardless of whether the statement was included in the JSP via directive or action. The difference is in when the included file is translated, not when it is executed.
+Pie Number of slices to send: Send
Ben and Bear collide heads... shake it out and move on...
+Pie Number of slices to send: Send
Yes, great minds think alike.....
...
.. and so do we
+Pie Number of slices to send: Send
like this:

<%
java.util.Calendar now = java.util.Calendar.getInstance();

int hour = now.get(java.util.Calendar.HOUR_OF_DAY);
int minute = now.get(java.util.Calendar.MINUTE);
int second = now.get(java.util.Calendar.SECOND);

if (hour<10)
out.println("0" + hour);
else
out.println(hour);

out.println(":");

if (minute<10)
out.println("0" + minute);
else
out.println(minute);

out.println(":");

if (second<10)
out.println("0" + second);
else
out.println(second);
%>


Cesar
+Pie Number of slices to send: Send
Right, as Ben and I pointed out. That code will get translated at different times (and in different places) depending upon whether you use the include directive or action, but it still executes when the page is accessed.
+Pie Number of slices to send: Send
Look at the generated servlet code.
If you're using Tomcat it's under TOMCAT_HOME/work...

You'll see exactly what's going on.
+Pie Number of slices to send: Send
Yeah I did it (don't ask me why I didn't before;-). Both are right. What the container inlines in compilation time is the *code* not the *result*. That is why we always get updated times.

Thank you guys. You're masters.

Cesar
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1008 times.
Similar Threads
What is the differences between 2 includes of JSP?
<%@ include file="invoiceFtpPush.xhtml" %> in jsp
getting output from servlet
difference between include directive &action tag
JSP include action
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:40:20.