This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we dynamically load servlet?

 
Mohit Mehta
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohit Mehta
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can web application have control on life cycle of servlet which is part of that application?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic