how to have multiple instance of the same servlet on tomcat

I write a servlet and this servlet must to be instanced many times in the same server. How to do that ? Is that some alias or parameter to define many "web servers" from the same servlet (with different parameters)

Thanks for your help

Thierry Vorms


You can't. The servlet specification dictates that only one instance of a servlet class should exist.

But it would be wrong to do otherwise. Don't use instance-variables in servlets. If you needs something like that - use session or request attributes


your question isn't terribly well worded, but will try and interpolate to see if I can figure out what you mean.

  1. if you mean have the same class used as many servlets - you can do this. e.g you could have a servlet class called ColorServlet, and an init param that tells it what color to serve up. Then you can have this referenced twice in the same web.xml file with two different servlet-mapping entries to two url patterns, e.g /myapp/color/blue and /myapp/color/red. You use an init-param section in the XML to set startup parameters for your servlet Have a look Here

  2. if you mean expose additional HTTP listeners on different ports - yup - this is also do-able, just have more 'connector' elements in your server.xml - they will all share your code, so you can serve the same servlet from multiple 'servers'