my dog learned polymorphism*
The moose likes JSP and the fly likes Taglib Scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Taglib Scope" Watch "Taglib Scope" New topic
Author

Taglib Scope

Subliner Kemp
Greenhorn

Joined: Nov 06, 2012
Posts: 6
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

What kind of tag are you writing? Classic or simple? The rules on how instance are created and shared differ.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Subliner Kemp
Greenhorn

Joined: Nov 06, 2012
Posts: 6
not sure what is classic taglib, but my class is as below:



JSP page declaration as below:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

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

Joined: Nov 06, 2012
Posts: 6
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

Joined: Apr 08, 2009
Posts: 298
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Taglib Scope