Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Tomcat , Servlet and Singleton  RSS feed

 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's something related to servlet and singleton.I have used tomcat for this verification.I have recently started to learn about design patterns.To my best of knowledge Singleton maintains a single instance of a class for one JVM.
So to verify this I have created a singletom class having a int varialbe a with default value 0.Then created one servlet with overriden init method .In init I have grabbed a instance of the singleton class and set the integet val as 10.In the web.xml I have set the load-on-startup as 1 , so that this gets loaded as soon as the context raises.I have deployed this serlvet in context A. (/A)

Secondly I have created another servlet in which I have overriden the service method.In this also I grabbed a instance of that singleton and print the value of the integer variable.I have deployed this servlet in context B(/B).

I found 0 ...but I expected 10 as this class has been already created as the value of the variable has been set to 10.Now its showing 0 not 10 ..that means that the JVM is having 2 instances of the singleton class.

How can this be possible...there should be one instance of the singleton.
There is something to do with the context in which I am deploying the servler.But singlon has nothing to do with the context..it should have one instance in one JVM.(nothing to do with the context.)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
But singlon has nothing to do with the context..it should have one instance in one JVM.(nothing to do with the context.)


Not entirely true.

Singletons are per classloader, not per JVM.
Tomcat has a separate classloader for each context (and also separate classloaders for JSPs).

If you put your singleton class in the tomcat/shared/classes directory, you will probably see the results you were expecting (all contexts would share the same static variable) because your class would be loaded by a common classloader.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

I have read you post along with the tomcat's classloading.Thanks a lot, it really helped a lot.
I have posted a similar stuff ..could you please see that and tell whether this is also due to the same reason.

http://www.coderanch.com/t/63084/open-source/Interesting-log

Many thanks in advance.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[BPSouther: Removed all thread hi-jacking posts]
Chetan,
You've been a member for long enough and have posted here enough
times to know that hijacking other people's threads is an extrememly rude
thing to do.
Please don't ever do that again.

If you have a question about something, ask in a new thread.
[ September 15, 2006: Message edited by: Ben Souther ]
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ben Souther, sorry for that. Henceforward I will take care of it.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there is anything wrong I have done in JavaRanch..
let me know
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
Is there is anything wrong I have done in JavaRanch..
let me know


No, someone else hi-jacked this thread (asked a different question inside it, leading the conversation away from your original question).
I, at first didn't notice that it was a different person asking the 2nd question and answered it; dragging the thread further from your originnal question.

Once I noticed this, I removed all of the posts that were not part of the original discussion and asked that person not to do this anymore.

You're fine.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!