views:

147

answers:

1

What are the advantages/disadvantages with regards to the fact that Tomcat only creates one instance of a Servlet class to handle all requests for a JSP/servlet?

+2  A: 

This is not Tomcat-specific, it's just conform the Sun Java Servlet API specification. You can however go around this by letting the servlet implement the deprecated SingleThreadModel interface.

Using a single instance applicationwide has the huge benfit that there's no means of overhead of creating a new instance on every request in busy environments. And the disadvantages? No one comes to mind. It makes perfectly sense.

BalusC
What about an "extremely heavy" load on the server? Would it be better to have several instances if one is getting bogged down with too many requests?
Lirik
That's no problem. Java is multithreaded. The same instance can be used simultaneously by multiple threads.
BalusC
...as long as you write threadsafe code yourself ;) I.e. do not assign request/session scoped data as instance variables. Also see this answer: http://stackoverflow.com/questions/2183974/difference-each-instance-of-servlet-and-each-thread-of-servlet-in-servlets/2184147#2184147
BalusC