• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Startup of servlets/ JVM:s

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... 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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to share resources between servlets, like database connections, you can put the resource as an context attribute.
 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Bill, I always address them as /servlet/Servletname.
Is this wrong?
// Andreas
 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic