
Servlet Tutorial will help you to learn servlet in details. Servlet is a Java program that runs within a web server. When we make a request to a web client, usually HTTP, over the Hypertext Transfer Protocol, the servlet receives and responds to our requests.
By using Servlet, you can collect input from users by using web forms, you can retrieve records from a database or any other source, and you can create web pages dynamically.
Servlets enable web applications to run on the Java Virtual Machine, which is a core part of the Java EE platform. Servlets run on the server-side and are capable to handle complex requests made to a web server.
Table of Contents
Applications of Servlet
There are following main applications of the Servlet. In other words, you can do following works by using servlet.
- Servlet takes less memory and less execution time.
- It reads the data sent by the browsers.
- By using servlet you can process the data and generate the results.
- By using servlet you can send the HTTP response to the clients.
Advantages of Servlet
There are following advantages of the using servlet
- Better performance
- Robust and secure
- Portability
- Inexpensive
- Efficiency
- Extensibility
- Persistent
- Server and Protocol Independent
Lifecycle of a Servlet (Servlet Lifecycle)
Servlet lifecycle is the whole process of its creation till destruction. The servlet container manages the life cycle of the servlet. The container uses the Servlet interface to understand the servlet object and manage it.
- Loading: The servlet class loading.
- Instantiation: The servlet instantiation.
- Initialization: The Servlet initialization with the init() method.
- Request Processing: The servlet calls service() method to process a client’s request.
- Deinstantiation: Destroys the servlet and becomes eligible for the JVM’s garbage collector.
Servlet Loading
When the web server (such as Apache Tomcat) starts up, the servlet container deploys and loads all the servlets.
Servlet Instantiation
The server creates a instance when code for a servlet is loaded. That instance handles every request made of the servlet.
Servlet Initialization
After instantiation, the init() method is called to initialize the servlet. This method is called only once in the entire lifecycle.
public interface Servlet {
public void init(ServletConfig config) throws ServletException;
}
Servlet Request Processing
When the servlet loading and initialization is complete, the web server is ready to receive requests. When a web server receives a request for a particular servlet, the service() method of that servlet is invoked.
public interface Servlet {
public void service(ServletRequest req, ServletRequest res) throws ServletException, IOException;
}
Servlet Deinstantiation
The servlet container calls the destroy() method to remove the servlet.
public interface Servlet {
public void destroy();
}
In conclusion, In this Servlet Tutorial section, you have learned about the servlet, advantages and applications of servlet and most importantly the Lifecycle of the servlet.