0

I have a web application deployed on tomcat 5.5 server. I have a Singleton class. I want the object of singleton class to be available anywhere in the application. I am initializing this singleton object and other process using a servlet in init method and configuring web.xml setting load-on-startup. In the init method I am creating a new object of singleton class and I want this object to be available anywhere in application. How to do it ?

user123
  • 518
  • 12
  • 24

2 Answers2

0

Get the ServletContext and use setAttribute() to save your singleton. You can then use getAttribute() anywhere to get it since there will be exactly one Servlet context per web app.

You can get the context in most places via HttpServletRequest -> getSession() -> getServletContext().

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Ah. Don't use Java, then. There is no way to prevent a determined mind to manipulate the object (and yes, there are ways to change `final` fields ...) – Aaron Digulla Mar 27 '14 at 14:38
  • I know that was silly question but that is the requirement. I am working on a common project. I don't want other developers to manipulate common object – user123 Mar 27 '14 at 14:40
  • You can use Spring or Guice; that makes it pretty hard to modify a singleton after it has been created. Please be aware that any requirement like "no one must ever be able" always has hidden costs that you **will** have to pay. So you should carefully consider if anyone could accidentally replace the singleton. If not, then your precaution is probably just a waste of time. – Aaron Digulla Mar 27 '14 at 14:42
  • If you call it in the `init()` method, then it doesn't matter since there are no other threads which could access it. If you manipulate it later, you need to synchronize. See this question: http://stackoverflow.com/questions/616601/is-httpsession-thread-safe-are-set-get-attribute-thread-safe-operations – Aaron Digulla Mar 27 '14 at 14:53
  • 1
    In a clustered environment, the ServletContext is initialized on each node, so whatever you put there, it's not a singleton anymore. – Christian Schlichtherle Apr 07 '15 at 11:15
  • @ChristianSchlichtherle: What's the correct solution for a cluster? I thought J2EE clusters would automatically share some context but I don't know which one. – Aaron Digulla Apr 07 '15 at 11:33
-1

Store the value you want in a static field and then create static methods for accessing the data.

JustinKSU
  • 4,875
  • 2
  • 29
  • 51
  • -1 Global variables were bad 20 years ago and they are still bad :-) – Aaron Digulla Mar 27 '14 at 14:27
  • @AaronDigulla Can you suggest any better approach than this. The problem statement is. I want to initialize a singleton object and access that single ton object anywhere in my applicaiton – user123 Mar 27 '14 at 14:30
  • @user3172529: Did you see my answer? – Aaron Digulla Mar 27 '14 at 14:30
  • I think it depends on the singleton being stored. For instance, if it's the name of the application then it's fine. It also makes it easier to access in some frameworks for the ServletContext is not as easy to get. My answer meets the original questions requirements. – JustinKSU Mar 27 '14 at 15:57