Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Taglib Scope

 
Subliner Kemp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would like ask about the scope of a custom taglib's class.

I have an arraylist declared inside the taglib class, outside of methods (private ArrayList abc = new ArrayList()).
In the doStartTag method, I put an abc.add("something");
Subsequently, i output the size of abc to my console.
When the JSP which calls this tag is run, each time it is refreshed, the abc size grows.
I would like to believe that taglib classes are instances (of page scope), much like Java beans where it is set such that scope = "page".
However, in my case, the arraylist looks to be shared across all pages in the application. (I expect the arraylist to always remain as size 1, as each jsp has its own instance of the taglib class, thus the arraylist is declared independently for each of the JSP page)
Ironically, if i were to put a string variable in the taglib class, and set it to some value through 1 JSP page, and then tries to retrieve the value of that variable via another JSP, the value will not be shared.
So how is it so that the arraylist acts like an application-scope or static variable?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of tag are you writing? Classic or simple? The rules on how instance are created and shared differ.
 
Subliner Kemp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure what is classic taglib, but my class is as below:



JSP page declaration as below:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TagSupport is a "classic" tag mechanism from JSP 1. The rules about instantiating and sharing such tags are complex and various containers may interpret them differently. I know I ran into a lot of trouble with that back "in the days".

As of 2002, JSP 2 introduced simplified "simple tags" that extend from SimpleTagSupprt. The rules are a lot more straight-forward; essentially tags cannot be shared and every use instantiates a new object.

I'd suggest catching up with modern times and using simple tags rather than obsolete classic tags.
 
Subliner Kemp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear Bibeault, I see now.
Unfortunately, my current project is a legacy one, where the IDE and application server is restricted to older JSP standards.
So there is no firm way to say force a classic taglib to instantiate a new class (not shared) for each page I presume?
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid that problem you can override the release method in your class



You might also want to call this method from your Tag constructor. I am not 100% sure what the flow is but I think if you don't call it yourself it does not get called, someone that knows more about this please confirm/correct this statement!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic