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?

toolkit
  • 49,809
  • 17
  • 109
  • 135
Kiril
  • 39,672
  • 31
  • 167
  • 226

1 Answers1

3

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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? – Kiril Feb 18 '10 at 03:18
  • That's no problem. Java is multithreaded. The same instance can be used simultaneously by multiple threads. – BalusC Feb 18 '10 at 10:37
  • 1
    ...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 Feb 18 '10 at 22:29