Open In App

Servlet Architecture

Servlets are grouped under the Advanced Java tree that is used to create dynamic web applications. Servlets are robust, well scalable, and are primarily used in developing server-side applications. If we go a little back in time, we would be able to witness that before the introduction of servlets, CGI (Common Gateway Interface) was used. Among several indigenous tasks that a servlet is capable of doing, dynamically performing client requests and responses are most common. Other tasks that a servlet can do effectively are:

Types of Servlet

There are three potential ways in which we can employ to create a servlet:



Components of Servlet Architecture

Servlet Architecture contains the business logic to process all the requests made by client. Below is the high-level architecture diagram of servlet. Let’s see in brief, how does each component add to the working of a servlet.



1. Client

The client shown in the architecture above is the web browser and it primarily works as a medium that sends out HTTP requests over to the web server and the web server generates a response based on some processing in the servlet and the client further processes the response.

2. Web Server

Primary job of a web server is to process the requests and responses that a user sends over time and maintain how a web user would be able to access the files that has been hosted over the server. The server we are talking about here is a software which manages access to a centralized resource or service in a network. There are precisely two types of webservers:

3. Web Container

Web container is another typical component in servlet architecture which is responsible for communicating with the servlets. Two prime tasks of a web container are:

Web container sits at the server-side managing and handling all the requests that are coming in either from the servlets or from some JSP pages or potentially any other file system.

How does a Servlet Request flow?

Every servlet should override the following 3 methods namely:

  1. init(): To initalize/instantiate the servlet container.
  2. service(): This method acts like an intermediatory between the HTTP request and the business logic to serve that particular request.
  3. destroy(): This method is used to deallocate the memory allocated to the servlet.

These methods are used to process the request from the user.

Following are the steps in which a request flows through a servlet which can be observed in the architecture diagram:

Advantages

Disadvantages

As we already know Servlets are portable (platform/server independent) in nature and hence are a better option if we talk in terms of other scripting languages. They process the requests and responses dynamically. Whenever we are developing a web application where we need to coordinate with different existing protocols, servlets are preferred over other means because of its capability to support various protocols. At last, we can descend to a conclusion that employing a servlet could potentially be most suitable while developing a web application.

Article Tags :