开发者

How HttpServlet can create threads without implementing Runnable or extending thread

As we know, when servlet receivies a request, it creates a new thread and inside the new thread, the service method is invoked. So with only one Servlet instance, many threads are invoked.

What I didn't understand is how HttpServlet is able to create threads of its own instance without implementing runnable or extending thread?

Can any one please clarif开发者_JS百科y.


As we know, when servlet receivies a request, it creates a new thread and inside the new thread, the service method is invoked. So with only one Servlet instance, many threads are invoked.

In fact, that is INCORRECT. The web container typically maintains a bounded pool of worker threads to handle requests. New threads don't get created for each new request.

What I didn't understand is how HttpServlet is able to create threads of its own instance without implementing runnable or extending thread?

Basically, it doesn't. HttpServlet does not create threads, and it is not a thread or a runnable.

The web container has implementation specific classes that implement Runnable or extends Thread. These classes call the relevant methods on the (shared) Servlet instance.


This is handled by the Servlet Container (also called the Web Container). The Servlet Container is responsible for maintaining the Servlet Lifecycle.

http://en.wikipedia.org/wiki/Java_Servlet

Life cycle of a servlet

  1. The container calls the no-arg constructor.
  2. The Web container calls the init() method. This method initializes the servlet and must be called before life of a servlet, the init() method is called only once.
  3. After initialization, the servlet can service client requests. Each request is serviced in its own separate thread. The Web container calls the service() method of the servlet for every request. The service() method determines the kind of request being made and dispatches it to an appropriate method to handle the request. The developer of the servlet must provide an implementation for these methods. If a request for a method that is not implemented by the servlet is made, the method of the parent class is called, typically resulting in an error being returned to the requester.
  4. Finally, the Web container calls the destroy() method that takes the servlet out of service. The destroy() method, like init(), is called only once in the lifecycle of a servlet.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜