| 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: 14685
|
|
|
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: 14685
|
|
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: 56529
|
|
"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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: Difference between jsp:include and include directive.
|
|
|