Saturday, February 28, 2009

What is a Servlet and a Servlet Container ?


A servlet is a Java programming language class used to dynamically process client requests and provide responses.

The javax.servlet and javax.servlet.http packages provide the interfaces and classes used to write Java Servlets. Servlet Life-Cycle methods are defined in the Servlet interface.

Servlets are controlled by the Container.

The container's role in s servlet's life

1.Communication Support
Provides a way for the servlets to talk with the Web Server.

2.Lifecycle Management
Controls the life and death of servlets. Please refer the diagram below.

The container creates a HttpServletResponse and HttpServletRequest object when a user clicks on a link to a servlet. A new thread is created for each request and these two objects are passed to the servlet's service method. The servlet figures out which method to call (doGet or doPost) based on the HTTP method sent by the client. The Response is written by the response object and it goes through the container.The service method completes and the thread dies or goes to a container managed thread pool. The objects go out of scope and will be collected by the garbage collector. The client will get the response.

3.Multithreaded Support.
A new thread is created for each servlet request.

4.Declarative Security
You can use the XML Deployment descriptor to configure security settings.

5.JSP Support
Translates JSPs to Java


No comments: