What is a Servlet
Servlet is nothing but a Java Class which services HTTP request. Consider servlet as a Java Class used to extend the capabilities of servers that host applications via a request-response programming model.
Though servlets are not tied to a specific client-server protocol but they are mostly used with HTTP.
The only requirement for writing a servlet is that it should implement the
javax.servlet.Servlet interface which defines Life Cycle Methods of a servlet.
Servlets interact with the Servlet Engine running on the web server through requests and responses.
For better understanding, you can consider servlet a client program which could be a web browser or some other program that can make connections across the internet, accesses a web server and makes a request. This request is processed by the servlet engine that runs on the web server, which communicates with Servlet. The servlet in turn sends a response in HTTP form to the client.
Servlet Life Cycle
All servlets must implement(directly/indirectly) the
javax.servlet.Servlet interface so that they can run in a servlet engine.
The servlet engine is customized extension to a web server for processing servlets, built in conformance with the java servlet API by the web server vendor. The servlet engine provides network services, understands MIME requests and runs servlet containers.
The life cycle of a servlet contains :
- Loading of Servlet Class and create Instance of it.
- Servlet’s instantiation using init() method.
- Servlet’s initialization using service() method.
- Servlet’s destruction using destroy() method.
Have a look at the below diagram and then we will see the detail of each step:
Servlet Loading and Instantiation
The servlet engine instantiates and loads a servlet. This can occur 1. When engine starts 2. When it needs the servlet in order to respond to a request 3. Anything in between
The servlet engine (a) loads a servlet using a Java class loading facility. The servlet engine can load the servlet from:
– Local File System
– A Remote File System
– Network source
and (b) creates an instance by calling the no-args constructor.
After the servlet engine loads the servlet, the engine must initialize the servlet.
Initialization is a good time for a servlet to :
- Read any persistent data stored
- Initialize JDBC database connections
- Establish references to other costly resources by overriding the init() method
- Config itself
init() method takes a servlet config object (of type ServletConfig) as a parameter, which allows the servlet to access name-value parameters from the engine’s configuration information. The servlet config object also gives the servlet access to a servlet context object of type ServletContext.
Service of the Servlet
In this phase, servlet engine invokes the service method, passing request and response objects. So this is the phase where actual task gets performed. Clients send the request and service method returns the response back. For each request from client, the server spawns a new thread and calls the service. To get more detailed insight visit here.
This is where servlet stops serving the requests and is at the end stage of its life. destroy() method gets called only once in servlet’s life cycle. Here the activities like releasing the DB connections, closing n/w connections or any such activities can be performed. After destroy() is called, servlet is ready for the garbage collection.