This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I declare a variable in an included page?

 
Sandesh Tathare
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
--------------- welcome.jsp ---------------
<%! int counter = 5; %>
counter: <%= counter %>
</br>
<jsp:include page="index.jsp"/>
--------------- welcome.jsp ---------------
--------------- index.jsp ---------------
<%! int counter = 10; %>
Welcome to Index.jsp</br>
counter: <%= counter %>
--------------- index.jsp ---------------
When I request for welcome.jsp, following is output shown on browser:
counter: 5
Welcome to Index.jsp
counter: 10
I read that we can't declare a variable in an included page using include directive, if it is already declared in the including page since 2 page constitute a single translation unit.
So I was expecting an error instead of the above output.
Can any one throw some light on this?
Sandesh
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandesh,
As you are using jsp:include which is runtime player,i.e it will copy output of included jsp in outputstream at runtime there is no error at compile time.
Instead if you use
<%@ include file="Index.jsp/" %> you will get the error you want
Cheers
Praful
[ November 17, 2003: Message edited by: Praful Thakare ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Often called static and dynamic includes.
The static include (ie what you were trying to accomplish) is similar to the C programming language #include directive - all the code from the included source gets picked up and pasted into the original page. This single piece of code gets compiled and would cause the duplicate variable problem you wanted.
The dynamic include (ie what you actually had) generates two separate pieces of compiled code, they get run separately, and the output gets added together.
Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic