How would I take a Singleton object and map this to work inside of a JSP page??? I have a singleton class, EnvironmentDataBean, that returns an existing instance of the environment databean with EnvironmentDatabean.getInstance(). This works great inside of the classes, however if I want to do this within a JSP page, how can I do this with the JSP tags and all. Thanks! Dale
By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
A true singleton object should be stored in the servlet itself and not in a session, since sessions themselves are not singleton objects. In a JSP, you'd want to declare the object in class scope, like so: <%! static SingletonObject onlyOne = new SingletonObject(); %> Note the "<%!", which defines the following code at class scope instead of being within the service method. Don't forget, too, that to use this object safely, you have to either declare the page as not being threadsafe or use synchronized accessors! In <useBean> terms, the equivalent for a class-scope object is scope="application", which actually means that all servlets/JSPs in the application (on the samve JVM) would use the same object. An application-scope object is stored in the ServletContext, and the easiest way to do that is to have a startup servlet that instantiate's the object and stores it into the webapp's ServletContext. Note that this ups the threading ante even further, since now multiple servlets/JSPs can see the singleton and they ALL have to use it in a thread-safe manner. [ January 31, 2002: Message edited by: Tim Holloway ]
Customer surveys are for companies who didn't pay proper attention to begin with.
Joined: Nov 02, 2000
Well, I agree w/you that would you would need to keep it thread safe if the object was going to change. This one won't. This object will remain the same. I could have also made this an Interface and put all of the data in it final an static. Since the users will not be allowed to change the bean, I think this would work fine. -Dale PS: A Singleton is a object pattern that only allows one instance of the object to exists for the entire system at once. You can look up Singleton in this forum. You will find plenty of references to it. It is probably the most popular of all the patterns. [ February 05, 2002: Message edited by: Dale DeMott ]