RE: Share Java resources between two (or more) servlets
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:[EMAIL PROTECTED] 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 : [EMAIL PROTECTED] web : www.e-horizon.se phone : +46 8 524 201 80 mobile: +46 708 85 23 35 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Share Java resources between two (or more) servlets
Hi, See intermixed. -Original Message- From: Andreas Byström [mailto:[EMAIL PROTECTED] Sent: 7. mars 2003 10:20 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? Sure no problem as long as the singleton object is in the same classloader scope it should not be a problem. Each application is loaded with sepperate classloaders, so as long as the servlets are in the same application and the singleton is in it's classpath too it works. 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? Use a ServletContextListener to startup such objects, rather than the load-on-startup method of servlets. It has some benefits doing it that way, like, you can do some cleanup (if needed) when the context is destroyed. 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? Not really... How are you sending the requests ? -reynir - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Share Java resources between two (or more) servlets
These suggestions seems do to exactly what I want in a structured way. I just need to read more about how to set up the application and the context in the correct way. Do you have any example? Regards, Andreas -Original Message- From: Reynir Hübner [mailto:[EMAIL PROTECTED] Sent: den 7 mars 2003 11:54 To: Tomcat Users List Subject: RE: Share Java resources between two (or more) servlets Hi, See intermixed. -Original Message- From: Andreas Byström [mailto:[EMAIL PROTECTED] Sent: 7. mars 2003 10:20 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? Sure no problem as long as the singleton object is in the same classloader scope it should not be a problem. Each application is loaded with sepperate classloaders, so as long as the servlets are in the same application and the singleton is in it's classpath too it works. 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? Use a ServletContextListener to startup such objects, rather than the load-on-startup method of servlets. It has some benefits doing it that way, like, you can do some cleanup (if needed) when the context is destroyed. 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? Not really... How are you sending the requests ? -reynir - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Share Java resources between two (or more) servlets
You could use a GlobalNamingResources datasource db connection pool which would be available to all your servlets via a ResourceLink, rather than a servlet. -Original Message- From: Bill Lunnon [mailto:[EMAIL PROTECTED] Sent: 07 March 2003 11:52 To: Tomcat Users List Subject: RE: Share Java resources between two (or more) servlets 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:[EMAIL PROTECTED] 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 : [EMAIL PROTECTED] web : www.e-horizon.se phone : +46 8 524 201 80 mobile: +46 708 85 23 35 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Share Java resources between two (or more) servlets
Well it is not just a db connection pool that I want to share. It is about 10 singelton instances that I would like every servlet to share. // Andreas -Original Message- From: Roberts, Eric [mailto:[EMAIL PROTECTED] Sent: den 7 mars 2003 12:47 To: Tomcat Users List Subject: RE: Share Java resources between two (or more) servlets You could use a GlobalNamingResources datasource db connection pool which would be available to all your servlets via a ResourceLink, rather than a servlet. -Original Message- From: Bill Lunnon [mailto:[EMAIL PROTECTED] Sent: 07 March 2003 11:52 To: Tomcat Users List Subject: RE: Share Java resources between two (or more) servlets 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:[EMAIL PROTECTED] 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 : [EMAIL PROTECTED] web : www.e-horizon.se phone : +46 8 524 201 80 mobile: +46 708 85 23 35 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Share Java resources between two (or more) servlets
This thing with LifecycleListener seems nice. I have made an implementation of the LifecycleListener interface but I cant start tomcat. It complains that it cant find my class. The implementation is in the same jar file as the servlets, and starting the servlets is not a problem. Does this file need to be in some other classpath? This is the error I get: ERROR reading /var/tomcat4/conf/server.xml At Line 36 /Server/Service/Engine/Host/Context/Listener/ className=se.ehorizon.voipzon.provision.servlets.ContextListener debug=9 Catalina.start: java.lang.ClassNotFoundException: se.ehorizon.voipzon.provision.servlets.ContextListener java.lang.ClassNotFoundException: se.ehorizon.voipzon.provision.servlets.ContextListener at org.apache.catalina.loader.StandardClassLoader.loadClass(StandardClassLoader .java:1127) at org.apache.catalina.loader.StandardClassLoader.loadClass(StandardClassLoader .java:992) And this is my settings for the context (from server.xml) Host name=provadm debug=0 appBase=webapps unpackWARs=true Context path=/ docBase=/var/ehorizon/servlet/provisioning debug=0 reloadable=true Parameter name=config_file value=/etc/ehorizon/provisioning/provision.xml override=false/ Listener className=se.ehorizon.voipzon.provision.servlets.ContextListener debug=9 /Context /Host Regards, Andreas -Original Message- From: Reynir Hübner [mailto:[EMAIL PROTECTED] Sent: den 7 mars 2003 11:54 To: Tomcat Users List Subject: RE: Share Java resources between two (or more) servlets Hi, See intermixed. -Original Message- From: Andreas Byström [mailto:[EMAIL PROTECTED] Sent: 7. mars 2003 10:20 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? Sure no problem as long as the singleton object is in the same classloader scope it should not be a problem. Each application is loaded with sepperate classloaders, so as long as the servlets are in the same application and the singleton is in it's classpath too it works. 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? Use a ServletContextListener to startup such objects, rather than the load-on-startup method of servlets. It has some benefits doing it that way, like, you can do some cleanup (if needed) when the context is destroyed. 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? Not really... How are you sending the requests ? -reynir - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]