Servlets in Java development.

An introduction to servlets in Java Enterprise development.

Aditya Kumar
3 min readSep 20, 2021

What is Servlets?

Servlets is a server-side Java API that along with the server is used to handle the requests from web clients. It is a collection of classes and interfaces which along with core Java API used to process requests.

Why Servlets?

Communication with any website is series of requests and responses. To handle these requests we need to program our server so that it can handle these requests properly. We have the following option for server-side programming to handle web requests.

  1. Using Common Gateway Interface(CGI): CGI is a program that is used to create dynamic web pages.CGI creates a new process for every web request. Suppose if millions of requests are coming then it will slow down our system because we need more resources to start a new process.
  2. Other platforms like Django, ASP.NET, Express.js: Servlets maintain the platform-independent feature of Java language that's why it is more secure than other server-side technologies.
  3. Moreover, We are in the enterprise world of java development so we are restricted to use Java.

Common features of servlets.

It gives us API for server-side programming to build dynamic web pages.

Servlets create a separate thread for every coming request so our request is handled concurrently thus resources are utilized optimally.

It incorporates all features of the core Java language such as garbage collection, OOPs, multithreading, JVM features.

Servlets life cycle methods.

Servlets have commonly three life-cycle methods which are following.

  1. Init: This method initializes the servlet when a request is made for the first time. It is a place to accommodate initialization activity for the servlet. For example, we can initialize our database connection inside the init method and later pass it as an argument to the Data Access Object(DAO).
  2. The Service method: This method is invoked depending upon the kind of request. If servlet receives GET request then doGet method is invoked and if POST request is made then doPost method is called.
  3. The Destroy method: This method is called when the server is shut down. This is a final method in the lifecycle of Servlets.

The init and destroy method is called once in the life of Servlets while the service method is called for every incoming request.

Flow chart of execution of Servlets.

Servlets terminologies.

Servlet config and Servlet context: Servlet config contains unique information about every servlet while Servlet context is available for all servlets.

RequestDispatcher: It is used for the communication between servlets in the same application. It is used to forward the request to another JSP or HTML file.

Filter: Filter is used to providing the targetted resources depending upon the user and state of the application. Like, if we want to book a movie ticket then filter first t checks ticket is available or not, then will redirect the user to different pages. It is also used to authenticate users.

References:

https://www.geeksforgeeks.org/introduction-java-servlets

--

--

Aditya Kumar

Hi, I am Aditya. I studied at IIT BHU Varanasi , India. I am a Java developer. I partricipate in competitive programming.