You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Andreas Byström <an...@e-horizon.se> on 2003/03/07 11:20:20 UTC

Share Java resources between two (or more) servlets

Hi all!

I'm writing an application that uses (for now) 4 different servlets. I would
like all these servlets to share some java singelton object. Is this
possible? Do all servlets share the same jvm instance and thereby uses the
same singelton object?

I have one servlet that is set to load-on-startup, this servlet will create
the java object (db connections and other stuff) that I want all servlets to
share later on. This servlet is not used anymore, it hust listens for
shutdown and will not receive any requests. Is this how you should solve a
initialization problem or is there some other way?

When I run my application I start this servlet  (servlet1) as above. But
when I then invoke another servlet, servlet2, (that is not set to
load-on-startup) there is a strange behavior. Using my traces I can read
that it first invokes init on servlet1 again and then init on servlet2. If I
then sends a first reuquest to servlet3 it just does init on servlet3. If I
instead had sent the first request to 3 before the first request to servlet2
it is the same behavior but vice versa. when the request comes to servlet3
it does init on first 1 nd then 3. The request to servlet 2 now just incokes
init on servlet2. Have anyone a clue of you it can be like this?

// Andreas

____________________________________
Andreas Bystrom
Computer Engineer

e-horizon Streaming Technologies
Stadshusplatsen 2, 4th floor
Box 172
SE 14922 Nynashamn

mail  : andreas.bystrom@e-horizon.se
web   : www.e-horizon.se
phone : +46 8 524 201 80
mobile: +46 708 85 23 35
____________________________________


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: Share Java resources between two (or more) servlets

Posted by Bill Lunnon <bi...@mirrim.com.au>.
Andreas,

Firstly, the servlet with db connections should be created and added to the
ServletContext (hence not really a servlet). All other servlets can than
access this object via getServletContext.getAttribute() etc.
This servlet is defined as a servlet, loaded on initialisation. You must
release that the init() method of this servlet can be called multiple times,
especially if web.xml is changed. To avoid any dramas, make sure that the
init() is singleton (ie it only operates once). Same applies to the
destroy()

To be safe, make sure the get() and post() methods of the servlet do
something like redirect the response to another web page.

I know this approach works.

Hope this helps

Bill
-----Original Message-----
From: Andreas Byström [mailto:andreas.bystrom@e-horizon.se]
Sent: Friday, 7 March 2003 9:20 PM
To: Tomcat Users
Subject: Share Java resources between two (or more) servlets


Hi all!

I'm writing an application that uses (for now) 4 different servlets. I would
like all these servlets to share some java singelton object. Is this
possible? Do all servlets share the same jvm instance and thereby uses the
same singelton object?

I have one servlet that is set to load-on-startup, this servlet will create
the java object (db connections and other stuff) that I want all servlets to
share later on. This servlet is not used anymore, it hust listens for
shutdown and will not receive any requests. Is this how you should solve a
initialization problem or is there some other way?

When I run my application I start this servlet  (servlet1) as above. But
when I then invoke another servlet, servlet2, (that is not set to
load-on-startup) there is a strange behavior. Using my traces I can read
that it first invokes init on servlet1 again and then init on servlet2. If I
then sends a first reuquest to servlet3 it just does init on servlet3. If I
instead had sent the first request to 3 before the first request to servlet2
it is the same behavior but vice versa. when the request comes to servlet3
it does init on first 1 nd then 3. The request to servlet 2 now just incokes
init on servlet2. Have anyone a clue of you it can be like this?

// Andreas

____________________________________
Andreas Bystrom
Computer Engineer

e-horizon Streaming Technologies
Stadshusplatsen 2, 4th floor
Box 172
SE 14922 Nynashamn

mail  : andreas.bystrom@e-horizon.se
web   : www.e-horizon.se
phone : +46 8 524 201 80
mobile: +46 708 85 23 35
____________________________________


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org