• Post Reply Bookmark Topic Watch Topic
  • New Topic

include (directive x action)  RSS feed

 
Cesar Olavo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben and Bear collide heads... shake it out and move on...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, great minds think alike.....
...
.. and so do we
 
Cesar Olavo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cesar Olavo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!