• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

(URGENT) Singletons and JSP pages

 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<jsp:useBean > ?
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nah.. that won't work. I just fully qualified the bean. So now I have
com.amfam.isagent.afsllc.EnvironmentDatabean envBean = com.amfam.isagent.afsllc.EnvironmentDatabean.getInstance();
-Regards
Dale
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well once U do that & put it into the scope U wanted to, can't you reference it using?
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I second our bartender:
<jsp:useBean name="envBean" type="com.amfam.isagent.afsllc.EnvironmentDatabean" scope="session"/>
the tag above should do the trick.

OR.
(com.amfam.isagent.afsllc.EnvironmentDatabean) session.getAttribute("nameOfObject");
then assign the type returned to same session object,which is available in Ur JSP

Question.
What are singleton classes/objects and how/why are they used in wedapps.
Ideas and references will be appreciated.
cheers
[ January 31, 2002: Message edited by: Gabriel Fox ]
 
Saloon Keeper
Posts: 28757
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic