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.
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.
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
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
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)
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
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.
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)
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?
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)
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.
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.
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.
Post by:autobot
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