Forums Register Login

Include directive

+Pie Number of slices to send: Send
As per understanding the jsp include directive(ie <%@ include ) is processed at jsp translation time. This would mean that the code from the included file would be copied over to the including file and then the jsp would be compiled as a single servlet .class file.

This further means that if i make any changes in the included file then that change is not visible to the jsp and the jsp would still show the old file behavior only.

I tried a small example and I am seeing behavior where the included file is reflected in the jsp as soon as I edit the included file.

e.g Here Dummy.jsp includes include.html

step 1. I start the server and access Dummy.jsp
It shows the content of include.html
step 2. I made changes to include.html
I observe that the Dummy.jsp is now showing the latest content from include.html(I didn't redeploy or restart the server)

Can anynody tell me if I understanding of include directive is wrong. Also let me know what I am missing.

Code of Dummy.jsp is


Code of include.html
+Pie Number of slices to send: Send
The Servlet container that compiles-and-runs JSP file sees changes to content then it re-compiles the modified files. This in turn will cause the changes to be reflected in the next reload.
+Pie Number of slices to send: Send
I am not sure I am following the answer. I wanted to know following:
1. As per the JSP specification, what is the behavior expected of include directive
2. Is a servlet container expected to track for changes to content included through include directive

I wanted to know the reason behind this behavior which I mentioned in the original post
+Pie Number of slices to send: Send
What servlet/JSP container are using? Some containers can automatically to automatically detect when a JSP has changed, and I suspect your container does have a setting that has turned this feature on.

Pankaj Kumarkk wrote:I am not sure I am following the answer. I wanted to know following:
1. As per the JSP specification, what is the behavior expected of include directive
2. Is a servlet container expected to track for changes to content included through include directive

I wanted to know the reason behind this behavior which I mentioned in the original post

 
+Pie Number of slices to send: Send
I am using tomcat.
However this should not matter as I am changing a html file and not jsp.

As per my understanding a jsp will re-translate and re-compile only if it's modified timestamp has changed.

Just to restate the issue: I have a jsp which include<%@s a html file(using include directtive ie <%@ include)
Question: Will the jsp pick up the latest html as and when I change the html file(without retstart of server)
+Pie Number of slices to send: Send
 

Pankaj Kumarkk wrote:Question: Will the jsp pick up the latest html as and when I change the html file(without retstart of server)



What happened when you tried it?
+Pie Number of slices to send: Send
I am seeing that the jsp is picking up latest html file as soon as I modify the html file. I have given the sample jsp and html file code also and it can be tried very quickly
+Pie Number of slices to send: Send
So you have seen the behavior with your own eyes. What's the issue?
+Pie Number of slices to send: Send
The issue is with understanding of the concept. Since include directive are processed at translation time therefore the jsp should not pick a html(changed at runtime). Is there a gap in understanding or does the jsp/servlet spec say this.
+Pie Number of slices to send: Send
Changes to the JSP can also happen at run time, after the original translation, and yet they get updated.
+Pie Number of slices to send: Send
I am not clear of the reply. Can you please elaborate.
Are you saying that the jsp will get retranslated again even if there are no changes to the jsp file. (Note: In the current case the html file is getting changed at runtime and not jsp)
+Pie Number of slices to send: Send
No, that's not what I'm saying.

I'm saying that the container can detect when a JSP file has changed and re-translate it when that happens, why does it surprise you that it can do the same for other files?
+Pie Number of slices to send: Send
I am surprised because that's not what jsp/servlet spec(as per my understanding) says. I would like to know what is the correct behavior for a include directive.

In my books, the spec drive the implementation behavior and not the other way around.
So either the spec are wrong(which i am sure is not the case) or my understanding of spec is wrong or tomcat implementation is wrong(highly unlikely)

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

I think I read about this behavior in "Head First Servlets & JSP" book and there it was said that new Tomcat versions are "smart" enough to detect that the included parts in jsp(using include directive) have changed and the container is taking care of this change by regenerating the servlet code from the jsp and the new included component.

I do not remember in which version of Tomcat this feature was included but if I am not wrong 5.x and 6.x are already "smart" containers.
In the book was explained that the specification is not guaranting that this behavior will be present, so it depends on the used container.
If you cannot find the source in the book just say and I will try to dig it up for you.

Regards
+Pie Number of slices to send: Send
Thanks for the explanation. If include directive behavior is vendor dependent then what is the difference between include directive and jsp:include action. Why I would chose one over another.
I thought that i should chose include directive if I have some static content as the performance of include directive is faster than jsp:action (as one is translation time and other is runtime)

Then the only reason for choosing jsp:action would be when your jsp page size is too big(>64k) and you want to seperate it into multiple jsps.
+Pie Number of slices to send: Send
I think that even after introducing this auto update functionality in Tomcat container the performance is still the same(although I cannot confirm that with examples).
Using include directive still creates servlet that includes the whole code inside. And <jsp:include> standard action is still making runtime call. The only difference is that every time when you change some of the included elements(pages) the jsp pages will get automatically translated to servlets and compiled.

I don't think that you will change the code in the included static html pages very often, so the performance will suffer only when you make such refactoring.
Of course every situation should be analyzed separately.
You learn how to close your eyes and tell yourself "this just isn't really happening to 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 1854 times.
Similar Threads
request.getAttribute Not Displaying Anything In JSP
calling jsp directl from HTML form
include file Directive not working properly. Variable not resolved in jsp
Converting request parameters to JSP tag-file attributes
include directive doesn't work
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:29:48.