It's not a secret anymore!
The moose likes Servlets and the fly likes Preloaded servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Preloaded servlet" Watch "Preloaded servlet" New topic

Preloaded servlet


Joined: Dec 10, 2002
Posts: 5
Hi All,
I have a requirement in which I need to write the connection pool for the database in one servlet, say servlet1. Now this servlet acts as a Database connection pool for all the other servlets present in the same application. The other servlets can collect the database connection from this servlet.
My concern, is how to preload the servlet1, before the request to the other servlets is send. When the request is send to the servlet then an instance of that servlet is created in the server, but for the servlet1 no request is send by the client directly.

"Intelligence without ambition is a bird without wings."
Karthik Banda

Joined: Oct 24, 2002
Posts: 9
Hi ,
You can load a servlet instance by using java.reflect package as we do in servlet collaboration. This following may help you out..
psuedocode is as:
Servlet ser = ServletContext ctx.getServlet("servletName");
Class xx=Class.forName(ser.getClass().toString());
Object o=xx.newInstance();
u will find a few exceptions like InstantiationException, IllegalAccessException, ClassNotFoundException accordingly, in case of any problem...
try with this and let me know the result at

Sun Certified Java Programmer<br />Sun Certified Web Component Developer <br />Sun Certified Business Component Developer<br />BEA Certified Weblogic Server Specialist<br /> <br />Never Stop Questioning......
Dominic Paquette
Ranch Hand

Joined: Dec 13, 2002
Posts: 64
I think that the container(ex.Tomcat) will create an instance of the servlet when it starts up.
Correct me if I'm wrong
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
My question is, why are you writing your own connection pool in this day and age? ALL of the commercial vendors provide their own connection pooling, and there are at least a dozen open-source implementations of connection pooling!

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at for other WebSphere information.
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
You can use the <load-on-startup> tag in the deployment descriptor to instruct the container to load and initialise the servlet as soon as it (the container) is started.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Marty Hall
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
My concern, is how to preload the servlet1, before the request to the other servlets is send.

If servlet1 really needs to be a servlet, use the following in web.xml:

However, if the only purpose of servlet1 is that initialization (ie, servlet1 never handles real requests), then you can use a ServletContextListener instead. More straightforward than a "servlet" that is not really a servlet but just an initializor-thing.
- Marty

Java training and consulting
I agree. Here's the link:
subject: Preloaded servlet
jQuery in Action, 3rd edition