aspose file tools*
The moose likes Servlets and the fly likes Can we dynamically load servlet? 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 "Can we dynamically load servlet?" Watch "Can we dynamically load servlet?" New topic
Author

Can we dynamically load servlet?

Mohit Mehta
Greenhorn

Joined: Jan 24, 2005
Posts: 25
Hi

Can we dynamicaly load servelt without giving entry in web.xml?

In our web application we have sevral servlets in our application and at particular point of time few of them might be active and few of them diactivate means their service are not available and this acitivation and deactivation can be done by admin of application.

For example this point of time say 3 servelts are available for service and
4 are not available and admin changes activ and deactivate flag and activates one more servlet. now can we[application] automatically load or add that newly activated servlet to application context?


Regards,<br />BulletProof Monk.<br /> <br />"Nthing the Mind of man can Conceive<br />and Believe,It can achieve"
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Lookup the "Front Controller"

With it, you only need to map one servlet in your web.xml file.
It, takes care of loading all the others.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mohit Mehta
Greenhorn

Joined: Jan 24, 2005
Posts: 25
Thank you for your such quick reply.

I went through Front Controler Pattern.

That talks about one controler servlet which internally decides which to load.
so
it address URL pattern like
http://some.server.com/process

here process - points to controller servlet.

but we have address URL pattern like
http://some.server.com/base/serviceproviderServelt1


here
base/serviceproviderServlet1 - points to serviceproviderServlet1

or some time url may come as

http://some.server.com/base/serviceproviderServelt2

base/serviceproviderServlet2 - points to serviceproviderServlet2

Now admin can have ability deactivate or activate this 'serviceproviderServlet2' at and point of time, that means when admin deactivt some servlet application need to unload deactivated servlet and
load activated servlet.

Can you tell me how can we achive this?

Thank you once agian..
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are several ways this is done.

One is to look for a form parameter and use that to determine which servlet to use.

Struts does this by mapping the extension ".do".
All requests that end in ".do" go to a controller servlet.
The servlet then reads the the URI, strips out the ".do" part, and forwards according to what's left.

Another way is to map all of your action servlets to a particular directory.
Such as:
actions/*
Then, in the controller, parse the URI and look for the last part of the URI and use that string to steer your command pattern.


I have a very crude and simple example of this (using form parameters) on my site.
http://simple.souther.us
Look for Simple Command.
Mohit Mehta
Greenhorn

Joined: Jan 24, 2005
Posts: 25
Thank a lot Ben.

I deploy SimpleServlet.war. as you can see there we have entry for SimpleServlet in web.xml.

Can you instantiate,load and unload this SimpleServlet without putting entry in web.xml?

but here in our application those servlets as some 3rd party servlets
that we allready have in side our appication but its instantiation and loading to application context should be in hand of application itself.

and i mention before
our URL address will be something like this
http://some.server.com/base/seriveProviderServlet1?dec=hello&title=bye&..

to such servlet we are passing some or other parameters.

Thanks once again Ben.

Please keep sending your suggestion.
Mohit Mehta
Greenhorn

Joined: Jan 24, 2005
Posts: 25
Can web application have control on life cycle of servlet which is part of that application?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Mohit Mehta:
Can web application have control on life cycle of servlet which is part of that application?



Not really but the front controller will give you the same results.
It looks, from your earlier post that you grabbed "SimpleServlet".

The project you should have grabbed was SimpleCommand.
This uses a servlet as a front controller for command objects which can be loaded and unloaded by the application.
Kamlesh Thakkar
Greenhorn

Joined: Aug 30, 2005
Posts: 7
dont we need an entry for every serlvet inside web.xml?
As far as I can recollect when we make an entry in web.xml , only then the servlet config object will be available to the servlet object...
If there is no entry in web.xml , how will the app differentiate what is a servlet and what are normal objects.


SCJP 1.4<br />SCWCD 1.4
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
We can use the invoker servlet for this case.We do not need to configure the servlet in the web.xml and use the invoker servlet for this.Make sure to activate the invoker servlet in the web.xml at /conf.


Rahul Bhattacharjee
LinkedIn - Blog
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Kamlesh Thakkar:
dont we need an entry for every serlvet inside web.xml?
As far as I can recollect when we make an entry in web.xml , only then the servlet config object will be available to the servlet object...
If there is no entry in web.xml , how will the app differentiate what is a servlet and what are normal objects.


You can pass setting (or even the whole config object if you choose) from the front controller to the other servlets (action classes).
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rahul Bhattacharjee:
We can use the invoker servlet for this case.We do not need to configure the servlet in the web.xml and use the invoker servlet for this.Make sure to activate the invoker servlet in the web.xml at /conf.


The invoker servlet is a Tomcat feature which has been, in effect, deprecated.
It still exists but it's servlet entry has been commented out of the config files.

There were a lot of reasons for this.
Rahul already mentioned one (You can't use init params configured in web.xml when using the Invoker).
To read the other reasons why you shouldn't use it, see:
http://faq.javaranch.com/view?InvokerServlet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we dynamically load servlet?