A servlet is a Java technology based web component, managed by a container, that
generates dynamic content.
A servlet can be considered as a tiny Java program which processes user request and generates dynamic content.
There are many other alternatives like php, asp .net or CGI, for developing dynamic web sites. benefit of using servlets
over other technologies is servlets are developed in Java, so it comes with all benefits of Java language and it
is platform independent.
Following are the some advantages of using servlets.
(1) They are generally much faster than CGI scripts.
(2) They use a standard API that is supported by many web servers.
(3) They have all the advantages of the Java programming language, including
ease of development and platform independence.
(4) They can access the large set of APIs available for the Java platform.
What is a Servlet Container?
Servlet container is a runtime environment, which implements servlet API and manages life cycle of servlet components.
Container is responsible for instantiating, invoking, and destroying servlet components.
One example of container is Apache Tomcat which is an opensource container.
Servlet Life Cycle :
A servlet is managed through a well defined life cycle which defines how it is loaded,
instantiated and initialized, handles requests from clients, and how it is taken out of
The servlet life cycle is consist of following phases.
During this phase container creates a new instance of servlet. this is the first phase of servlet life cycle.
After the servlet object is instantiated, the container initializes the servlet before
it can handle requests from clients. during this phase container calls the init() method of servlet.
This is the second phase of servlet life cycle.
(3) Request Handling
Actual request processing and response generation occurs during this phase.
Container calls service() method of servlet component and passes ServletRequest and ServletResponse objects as parameter.
This is the last phase of servlet life cycle. during this phase container calls destroy() method of servlet component.
After this phase servlet is removed from service. this happens when server is shutting down, or server wants to free some memory.
All of this life cycle methods are defined in Servlet interface.
following methods are defined in Servlet interface
public void init(ServletConfig config)
public ServletConfig getServletConfig()
public void service(ServletRequest req,ServletResponse res)
public void destroy()
Current version of servlet specification is 2.5
This article can be republished with following link
Java articles and tutorials