Servlet
Servlet
Loading a Servlet.
Request handling.
1. Initializing the context, on configuring the Servlet with a zero or positive integer value.
2. If the Servlet is not preceding stage, it may delay the loading process until the Web container
determines that this Servlet is needed to service a request.
•Instantiation : Creates an instance of the Servlet. To create a new instance of the Servlet, the
container uses the no-argument constructor.
2.Initialializing a Servlet
After the Servlet is instantiated successfully, the Servlet container initializes the instantiated Servlet object. The container initializes the
Servlet object by invoking the Servlet.init(ServletConfig) method which accepts ServletConfig object reference as parameter.The
Servlet container invokes the Servlet.init(ServletConfig) method only once, immediately after the Servlet.init(ServletConfig) object is
instantiated successfully. This method is used to initialize the resources, such as JDBC datasource.
• Now, if the Servlet fails to initialize, then it informs the Servlet container by throwing
the ServletException or UnavailableException.
3.Handling request
After initialization, the Servlet instance is ready to serve the client requests. The Servlet container performs the following operations
when the Servlet instance is located to service a request :
• It creates the ServletRequest and ServletResponse objects. In this case, if this is a HTTP request, then the Web container
creates HttpServletRequest and HttpServletResponse objects which are subtypes of
the ServletRequest and ServletResponse objects respectively.
• After creating the request and response objects it invokes the Servlet.service(ServletRequest, ServletResponse) method by
passing the request and response objects.
Destroying a Servlet
• When a Servlet container decides to destroy the Servlet, it performs the following operations,It allows all
the threads currently running in the service method of the Servlet instance to complete their jobs and get
released.
• After currently running threads have completed their jobs, the Servlet container calls
the destroy() method on the Servlet instance.
After the destroy() method is executed, the Servlet container releases all the references of this Servlet
instance so that it becomes eligible for garbage collection.
Servlet Life Cycle Methods
There are three life cycle methods of a Servlet :
•init()
•service()
•destroy()
init() method:
• This method is called only once to load the servlet.Since it is called only once in it’s
lifetime,therefore “connected architecture” code is written inside it because we only want once
to get connected with the database.
Example:
public void init() throws ServletException {
// Initialization code
System.out.println("Servlet Initialized");
}
Service() method:
Request:
Request parameter are useful inorder to retrive the input data from the html form.
Response:
Response object is mainly useful inorder to provide response to the client based upon the request.
Example code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("Hello from Servlet!");
}
Destroy() method:
• The destroy() method is called only once.
• This method performs various tasks such as closing connection with the database, releasing memory allocated to
the servlet, releasing resources that are allocated to the servlet and other cleanup activities.
• When this method is called, the garbage collector comes into action.
Example code:
public void destroy() {
// Cleanup code
System.out.println("Servlet Destroyed");
}
Example Code:
// Java program to show servlet example
// Importing required Java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Initializing servlet
public void init() throws ServletException
{
output = "Advance Java Concepts";
}