This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JSP and the fly likes Difference between jsp:include and include directive. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Difference between jsp:include and include directive." Watch "Difference between jsp:include and include directive." New topic
Author

Difference between jsp:include and include directive.

Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112
Hi friends,
I have read many books regarding the usage of jsp:include tag and include directive. But, i am unable to get an in-depth idea (purpose) of these tags. In what situations will i probably choose a one among them.

Can any one, present me with a real time example instead of a bookish example.

Thanks,
Venkat
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Could this FAQ help ?


[My Blog]
All roads lead to JavaRanch
Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112
Thanks for replying buddy,

Could anyone, explain the same with a real time example.




Thanks in advance,
Venkat
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

A simple example would be with a header. If you include a page using the include directive, changes you make to the included file will not be reflected. If you include a page using the include action, changes made to the included file will be reflected.

header.jsp

directive.jsp

action.jsp


Accessing the directive.jsp several times should not update the time. (in Tomcat, it will change... a container which does not check the included files timestamp will not update it)
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
[Edit: thread hijack removed. al, please start new topics for your own questions, unless they are continuations of the same (not related) issues. I've spawned your question off into a new post.]
[ April 03, 2008: Message edited by: Bear Bibeault ]
Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112
hi thanks for reply,

Iam unable to get the above example. please anyone explain it in more detail.....


Regards,
venkat
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
I'll give it a shot. based on the above poster's example.
When I go to the directive.jsp page, this is what appears on my browser:

[Time now is Thu Apr 03 19:29:47 EDT 2008]

I then close the browser, and go back to it five minutes later, and it says the SAME thing (Well, according to the poster of the example, it should say the same thing, unless your container checks for timestamps).

[Time now is Thu Apr 03 19:29:47 EDT 2008]. SAME as before

On the other hand, if I did the same thing with the action.jsp, the time would change.

[Time now is Thu Apr 03 19:29:47 EDT 2008] first time
[Time now is Thu Apr 03 19:34:47 EDT 2008] second time, accessed five minutes later


The reasoning for this is how the container handles the directive include(<%@ include...) versus how the container handles the include standard action (<jsp:include...).
It has to do with how the jsp gets turned into a servlet.

When using the directive, the code from the included jsp page (header.jsp) gets shoved into the directive.jsp, then the container
turns it into a single servlet.

On the other hand, using jsp:include, the code from the included jsp page (header.jsp) isn't shoved into action.jsp. Instead, the container
generates two servlets (one for header.jsp and one for action.jsp). A method is then placed into the servlet created from the action.jsp. This method
tells the container to call the servlet created from the header.jsp. The container then combines both results into one page.
Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112
Thanks for replying,
Can anyone explain in what scenario will i choose a one among two ...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You've been able to get quite a few responses to your questions.

Why don't you give us a couple examples of what you think each of these would be good for? If you're examples are good, we'll let you know. If not someone might be able to point you to a good tutorial or book.
The folks here are happy to help someone out but we like to see some effort on your part too.

-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sbin Wang
Greenhorn

Joined: Feb 05, 2009
Posts: 1
Include directive can only be used to include static content. The value of the property "file" must be "path+ file name". The file must really exist, otherwise you get file not found exception at run time. The file could have any name: ".html", ".jps", or even ".foo".

You are not allowed to have a servlet included: if you have SampleServlet, and this servlet could be accessed via ".../SampleServlet". But if you try to include this servlet with include directive like file="/SampleServlet" , you get file not found error at run time.

But including servlet with jsp:include action is ok: page="/SampleServlet". jsp include action could be used to include not only static but also dynamic content.




Sujai Kaarthik
Ranch Hand

Joined: Dec 29, 2008
Posts: 162
@ include is static include. It is inline inclusion. We cannot have a dynamic filename for directive include. <jsp:include> is dynamic include. Here the included file will be processed as a separate file and the response will be included. We can have a dynamic filename for <jsp:include>


Example:
You can include a file to <jsp:include> that will update the cricket scores frequently which you cannot do with @ include


The greater danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

"sss wang", please check your private messages for an important administrative matter.

And both sss and Sujai Kaarthik, please read this for information in relying to older, out-dated posts.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Consider Paul's rocket mass heater.
 
subject: Difference between jsp:include and include directive.
 
Similar Threads
Need solution for a query on JSP
Problem with include tag
Problem in Include File
diff between include action and include directive
Include directive