• Post Reply Bookmark Topic Watch Topic
  • New Topic

Advise - Servlet + Understanding Thread and Instance  RSS feed

 
Ken Flor
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

can I check.

I have a servlet performing Form Handling. When I click submit, the form will invoke doPost.

so in the form, it is : <form id="selectionForm" name="selectionForm" method="post" action="#">

I know there is only 1 instance of the servlet. However, if in this case, will it start another thread or still the same old thread?

if my form changes to <form id="selectionForm" name="selectionForm" method="get" action="#?querystring1">, resulting in doGet method, will it starts another new thread?

I want everything to be in 1 thread so that it will not waste resource.

Plase advise.

Thanks
 
Watsh Rajneesh
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ken,

There is a single instance of servlet which executes in the context of multiple threads from the container managed pool of threads. This is how you can have concurrent client requests handled by the same servlet instance. In case you want to have your servlet instance always execute within a single thread context (and that the container does not maintain more than 1 thread for running your servlet's service methods) then you can choose to implement the javax.servlet.SingleThreadModel (not recommended though and has been deprecated as of Servlet API 2.4 Spec).

If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.

Since the container uses a pool of threads (with max and min boundaries configurable in container specific DDs) so there is no reason to worry for the resources consumed as you can tune the pool size to match the optimal number of concurrent clients your application needs to support empirically.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!