• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preloaded servlet

 
Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regards
Goyal
 
Karthik Banda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
{
try
{
Servlet ser = ServletContext ctx.getServlet("servletName");
Class xx=Class.forName(ser.getClass().toString());
Object o=xx.newInstance();
}
catch(E)
{
}
}
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
karthik@electricangels.com
 
Dominic Paquette
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the container(ex.Tomcat) will create an instance of the servlet when it starts up.
Correct me if I'm wrong
Dominic
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goyal
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.
Mark.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goyal,
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.
Thanks,
Cindy
 
Marty Hall
Author
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Cheers-
- Marty
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic