Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Includes  RSS feed

 
Usha Shah
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I have a JSP 1 which includes JSP 2 using the following tags
<%@include file="file2.jsp" %>
There are some local variables in JSP1 which i am trying to access after the jsp 2 inclusion but getting different values when multiple users login
Please can someone throw light on why this is happening ?
 
Marty Hall
Author
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP 1 which includes JSP 2 using the following tags
<%@include file="file2.jsp" %>
There are some local variables in JSP1 which i am trying to access after the jsp 2 inclusion but getting different values when multiple users login
Please can someone throw light on why this is happening ?

I think you will have to show us your code before we can offer helpful suggestions.
Also: why are you using the @include directive instead of the jsp:include action? Does file2.jsp define variables or methods that jsp1 accesses? If not, don't forget how much harder it is to maintain things with @include than with jsp:include.
Cheers-
- Marty
 
Matthew Webster
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe try
<jsp:include page="" />
 
Usha Shah
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends
Thanks for the responses, but i got the problem with the code
The JSP 1 was using member variables i.e. all the variables used in JSP1 were declared like this
<%! int i = 0 %>
and thats why their values were getting mixed when we were having multiple users accessing the site
thanks for the help once again !!
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Usha Shah:
The JSP 1 was using member variables i.e. all the variables used in JSP1 were declared like this
<%! int i = 0 %>
and thats why their values were getting mixed when we were having multiple users accessing the site

That's because the
<%! /*stuff*/ %>
directive format is a declaration that is treated as a class member. i.e. if I had

my instance variable 'bar' is not thread safe.
What you're trying to do is to include a variable by inlining it. (Note: using a <jsp:include ... /> directive won't even compile, much less run.) What you really wanted to do was simply declare your variable in a scriptlet, i.e. remove the bang:
<% int i = 0 %>
However, I would advise to reconsider this design; it will only cause great headaches down the line.
Consider if you change the declaration of "foo" to be "bar" in the included page. Your pages will run as before, but those that included it will not recompile until you touch them again. Since you may not touch them for months, it could be months later that your pages suddenly don't compile and run! And if someone else caused it (or had to track it down) than originally made the change it would be frustrating at the least.
Further, it simply makes your pages very difficult to understand.
If you have to do something like this, I strongly encourage writing a bean or a custom tag to do what you want to do with the included variable. It's not as difficult or complex as it sounds, and will save you (or whoever maintains your code)an immense amount of effort later.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!