This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Include directive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Include directive" Watch "Include directive" New topic
Author

Include directive

Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
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
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

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.


S.D. MADHAN
Not many get the right opportunity !
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
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
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
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)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

So you have seen the behavior with your own eyes. What's the issue?
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Changes to the JSP can also happen at run time, after the original translation, and yet they get updated.
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
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)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

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?
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
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)

Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
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.
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Include directive
 
Similar Threads
calling jsp directl from HTML form
request.getAttribute Not Displaying Anything In JSP
include directive doesn't work
Converting request parameters to JSP tag-file attributes
include file Directive not working properly. Variable not resolved in jsp