IntelliJ Java IDE
The moose likes JSP and the fly likes JSP Includes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "JSP Includes" Watch "JSP Includes" New topic
Author

JSP Includes

Usha Shah
Greenhorn

Joined: Dec 11, 2002
Posts: 10
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 ?


Usha Shah
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
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


Java training and consulting<br /><a href="http://www.coreservlets.com/" target="_blank" rel="nofollow">http://www.coreservlets.com/</a>
Matthew Webster
Ranch Hand

Joined: May 10, 2001
Posts: 51
Maybe try
<jsp:include page="" />


Matthew Webster - eudoxus@freeuk.com<br /><a href="http://www.matthewwebster.homeunix.net/" target="_blank" rel="nofollow">http://www.matthewwebster.homeunix.net/</a>
Usha Shah
Greenhorn

Joined: Dec 11, 2002
Posts: 10
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

Joined: Dec 19, 2002
Posts: 374
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.


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
 
 
subject: JSP Includes
 
Threads others viewed
JSP 2.1
How do I know the parent jsp?
Struts: includeactive in jsp
Including a JSP inside the doEndTag() method
include directive question in java black belt
IntelliJ Java IDE