File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Startup of servlets/ JVM:s Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Startup of servlets/ JVM:s" Watch "Startup of servlets/ JVM:s" New topic
Author

Startup of servlets/ JVM:s

Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
Hi!
My question is about how the servlets gets started and if they all end up in the same VM.
Some data:
I'm using Tomcat4 and have implemented 2 servlets
- StartupServlet (sets up the environment with the db and reads some config)
- AdminServlet (this is the servlet that gets called everytime from the web server)
The StartupServlet starts around 10 new threads.
*When I just start tomcat, I can see 12 java threads on the linux machine.
*When I set StartupServlet to <load-on-startup> I see 22 threads.
*When I then send the first request to the AdminServlet, I can see in the printouts that it first runs init on StartupServlet again and then on the AdminServlet. I now have 33 threads.
My question is if someone knows why it inits StartupServlet again? When it does that, will it be in the same JVM as the first init och StartupServlet? Have anyone a good tpi how to solve this without using the StartupSerlvet approach, i.e. I want to initialize the system before I get the first request to AdminServlet. In the future I will have more servlets like AdminServlet that can be called from a browser.
another question: if I have two connectors, one for http and one for https, will the servlets they start "share" the same JVM?
Any suggestions appriciated!
// Andreas
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
... if I have another servlet than AdminServlet, will that one be run in the "same" JVM as AdminServlet? I want to know if I can share some db connection pools and stuff
// Andreas
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
Very strange, I'm now using 3 servlets and this is what happens:
I load servlet 1 at startup and it runs its init.
When I then send a request to either servlet 2 or 3 it will first run init for servlet 1 and then init for servlet 2 or 3.
But it will not do this twice, if I for example send a request to servlet 2 (the first request into the system) it will do init for 1 and 2, but when I send a request to 3 it will only do init for 3 (and the other way around, if I first send to 3 it will do init for 3 and 1 but then it will only do it for 2 when I send a request to 2).
// Andreas
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Exactly how are you referring to servlet 2 and 3 in the request? Are these servlets configured in web.xml?
Bill
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
This is my web.xml (except beginning)
<web-app>
<context-param>
<param-name>config_file</param-name>
<param-value>/etc/ehorizon/provisioning/provision.xml</param-value>
</context-param>
<servlet>
<servlet-name>StartupServlet</servlet-name>
<servlet-class>se.ehorizon.voipzon.provision.servlets.StartupServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>AdminServlet</servlet-name>
<servlet-class>se.ehorizon.voipzon.provision.servlets.AdminServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SubscriberServlet</servlet-name>
<servlet-class>se.ehorizon.voipzon.provision.servlets.SubscriberServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StartupServlet</servlet-name>
<url-pattern>/StartupServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/AdminServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SubscriberServlet</servlet-name>
<url-pattern>/SubscriberServlet</url-pattern>
</servlet-mapping>
</web-app>

Kan it be so that the method load-on-startup loads the Startup servlet to default-context?
Servlet1 is StartupServlet, Servlet2 is AdminServlet and Servlet3 is SubscriberServlet
// Andreas
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Since they are all defined in the same web.xml they are in the same "web application", the same servlet context and the same JVM.
Now I am wondering how your URLs are addressing them. Do you ever use a URL with "/servlet/" in it?
Bill
Ivan Tamayo
Ranch Hand

Joined: Aug 13, 2001
Posts: 49
In order to share resources between servlets, like database connections, you can put the resource as an context attribute.
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
Yes Bill, I always address them as /servlet/Servletname.
Is this wrong?
// Andreas
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
Or to be more precise:
In the httpd/conf/httpd.conf I have mapped the webapp to the address /mypages/servlet so the webapp has its "virtual root" on this url.
So the URL that is called and in the end ends up in the servlets is something like "http://storstark.e-horizon.se/mypages/servlet/AdminServlet"
// Andreas
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I think your problem may stem from using the /servlet/whatever notation. See the Tomcat /conf/web.xml discussion of the "invoker" servlet.
I have heard of people getting exactly this "init is called extra times" symptom due to this usage.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Startup of servlets/ JVM:s