• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

include directive is not for dymanic content?

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On p.435 of Head First, mock exam question 12:

How would you include dynamic content in a JSP , similar to a server-side include (SSI) ?
A. <%@ include file="/segments/footer.jsp" %>
Option A is incorrect because it uses an include directive, which is for static includes that happen at transition time.


But I think option A is correct because on p.409:

But if the included thing is dynamic, and you are using the static include directive, does that mean that the dynamic stuff is evaluated only once?
Let's say you include a JSP that has an EL expression that calls the rollIt function that generates a random number.
Remember, with the include directive, that EL expression is simply copied into the including JSP. So each time that page is accessed, the EL expression runs and a new
random number is generated. Burn this in: with the include directive, the source of the included thing becomes part of the page with the include directive.


With this quote on p. 409, I think option A is correct.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Option A seems valid to me too but there might be a better answer in B,C,D,E which you do not provide. In multiple choice questions, you should always pick the best answer.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Himai,

I must admit that the question can be interpreted wrongly with that explanation. The answer in the book is correct, though.

The rule to put in your head:
dynamic content - <jsp:include page=" ..." >
static content - <%@ include file=" ..." >

By default the server won't refresh the static content. Just try it yourself and see what happens in you change the included jsp file (with the jsp:include directive it will work immediately, and with the static include ....)

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I created a TestBean.jsp :

And it includes this randNumGenerator.jsp:


The first random number on the first line is from the <%@include > directive while the second number on the second line is from <jsp:include>.

I ran TestBean.jsp and got the two random numbers.
I refresh the browser.
Then, I got another pair of random numbers.
firstRun.PNG
[Thumbnail for firstRun.PNG]
secondRun.PNG
[Thumbnail for secondRun.PNG]
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not exactly what I meant, try changing the content of the randNumGenerator.jsp now so that it produces something else than a random number. What do you see in the TestBean.jsp ?
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I add a count variable to the included jsp:


I got the same output for count = 1 whenever I refresh the browser.
counter.PNG
[Thumbnail for counter.PNG]
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frits Walraven wrote:Hi Himai,

I must admit that the question can be interpreted wrongly with that explanation. The answer in the book is correct, though.

The rule to put in your head:
dynamic content - <jsp:include page=" ..." >
static content - <%@ include file=" ..." >

By default the server won't refresh the static content. Just try it yourself and see what happens in you change the included jsp file (with the jsp:include directive it will work immediately, and with the static include ....)



Hey, thanks for that! I had to review code for a junior front end dev who was trying to implement recursion to render a simple tree in struts 2 via include files as per my instructions and the difference just jump right to my eyes given the fact that I read your original reply a few days ago, I guess javaranch is cool.

Thanks!
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the same output for count = 1 whenever I refresh the browser.

Most likely because you use the same jsp for both includes. try it again with different jsp's. Once it works change the contents of both jsps, and you will see that it won't refresh static included file immediately.
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Frit. This is what I tried:
Step 1:
I created a staticDemo.jsp

Step 2: I created a Count.jsp


The output is :
1
Test 1.
But when I update both Count.jsp and staticDemo.jsp like this:



I did not redeploy the updated jsp. I saved them in NetBean.
I refresh the browser.
The output is still:
1
Test 1
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is the behaviour I wanted you to see!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic