Servlet is a part of J2EE. As we know, in Java, J2EE is used to develop web
based applications, web services, enterprise software etc. Servlet is a Java based API which runs on Server machine. A Servlet is intercepted on a request it
receives, and generates a response according to the corresponding request.
Servlet is a Java class that extends the capabilities of a Server. Servlet can
be thought of as an Java Applet but, the difference between Servlet and Applet
that the Servlet runs on Web Server whereas Applet runs on Web Browser.
Life cycle of Servlet is managed by the three methods named init(),
service(), and destroy(). These methods are implemented by every Servlet and
they are called at specific time by the server.
init() : When a Servlet is called this method is
invoked first by the web container. A web container invoked this method to
initialize the Servlet instance. This method is called only once in the life
cycle of Servlet.
service() : Once a Servlet is initialized it can
service the client request. A web container calls the Servlet's service()
method for every request. To provide the service of each request a separate
thread is created.
destroy() : When a Servlet is required to stop the service a web container calls this method and the Servlet becomes out of
service. This method is called only once in the Life cycle of Servlet.
Versions Of Servlet
Servlet has been released with various versions. Current version of Servlet
(at the time of writing this section) is Servlet 3.0. Prior versions of Servlets
are listed below :