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 JSP and the fly likes Problem with include tag 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 » JSP
Bookmark "Problem with include tag" Watch "Problem with include tag" New topic
Author

Problem with include tag

Vaibhav Deshpande
Ranch Hand

Joined: Apr 26, 2002
Posts: 33
hi all,
In my haeder file i declared global variables i needed on every page,
can i use following include tag
<%@ includefile='./../../GEN/GEN/header.jsp'%>
when i am using above line of code on top of my page it work fine.
but when i try to use include as a jps include like the code below

<jsp:include page='../../GEN/GEN/header.jsp' flush='true'/>
but this line not working .
what the reason. can some one help me out
thanks,
vaibhav
amol re
Ranch Hand

Joined: Mar 21, 2002
Posts: 36

Thats because there is a slight difference between how the two includes work.
The include directive, ie. <%@ include file="..." %> is used at translation time. [When the translator generates servlet code from your jsp file]
At translation time, above directive is replaced by the contents of the file that you specify in the file attribute.
So, in your included file, if you are referring to a variable declared in the including file, it works because while compiling, the including and included file are part of the same servlet.
The inlude action, i.e. <jsp:include page="..." /> comes into action at request time.
So when you are using include action, the above line isnt replaced with the contents of the included page as in case1. Instead, it is replaced by requestDispatcher.include() call.
Two servlets are formed. One for the including page and the other for the included page.
At run time, when a request comes to first page which contains the <jsp:include.../>, internally, the container sends a request to the second page. the output of this second page replaces the <jsp:include...> tag that you write.
In this case if you notice, since there are two different servlets, variables declared in one will not be available in the second.
Hence the error that you are getting.
In a nutshell...

Include directive is
  • Includion of the page contents.
  • Happens at translation time.


  • Include action is
  • Inclusion of the output of the page.
  • Happens at request time.


  • Repercurssions:
    If you use directive:
  • Code in page 2 can use all the varilables and methods from page 1 because eventually both the pages are compiled into single servlet.
  • Slightly improved performance compared to action since there is no request time calling of requestdispatcher.include()
  • If your page 2 is going to change frequently, you yourself will have to take care of translate & compile page 1. Unless you do that, the changes in page2 wont be reflected in page1


  • If you use include action:
  • You are loosely coupling the two pages which arent dependent on each others variables.
  • Useful when you know your page 2 is going to change frequently.You dont have to worry about explicitly compiling page 1. It is not required.
  • You may face standard "Buffering issues".

  • Vaibhav Deshpande
    Ranch Hand

    Joined: Apr 26, 2002
    Posts: 33
    Thanks Amol its a useful info for me.
    vaibhav
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Problem with include tag
     
    Similar Threads
    include page directives
    Problem with include tag
    jsp:param usage
    Hoto include JSP page in JSF
    HELP! Weird Include problem!