Introduction to Servlet Architecture
Servlet architecture comes under a java programming language used to create dynamic web applications. Mainly servlets are used to develop server-side applications. Servlets are very robust and scalable. Before introducing servlets, CGI (common gateway interface) was used. Servlets are used to perform client request and response tasks dynamically. Servlets can be used to perform tasks like,
- Control the flow of the application.
- Generate dynamic web content.
- Server-side load balancing.
- implement business logic.
There are two types of Servlets- 1.Generic Servlets 2.HTTPServlets.servlets can be created in three possible ways (i)Implementing Servlet Interface (II)Extending Generic Servlet. (III)Extending HTTPServlet. Three life cycle methods available with servlets are init(), service() and destroy(). Every servlet should override these methods.
Components of Servlet Architecture
Below is the diagram to show how components working on servlet architecture.
In this architecture, the web browser acts as a Client. Client or user connected with a web browser. The client is responsible for sending requests or HttpRequest to the web server and processing the Web server’s responses.
2. Web Server
Web server controls how web user access hosted files, and it’s responsible for processing user request and responses. Here server is software it understands URLs and HTTP protocol. Whenever a browser needs to host a file on the webserver, process client request using an HTTP request; if it finds the requested file sends it back to the browser through HTTP Response. There are two types’ web servers Static and Dynamic webservers.in a static web server, it sends the file as it is, but in a dynamic web, the server-hosted file is updated before it is sent to the browser.
3. Web Container
A web container is a component in the webserver it interacts with Java servlets. A web container is responsible for managing the lifecycle of servlets, and it also performs the URL mapping task. Web container handles the requests of servlets, JSP and other files at the server-side. The important tasks performed by servlets are loading and unloading servlets, creating and managing requests and response objects, and performing servlet management’s overall task.
Servlet Request Flow
Following are the steps how servlet request has been processed consider the above diagram.
- The client sends a request.
- Web Server accepts the request and forwards it to the web container.
- Web container searches web.xml file for request URL pattern and gets the address of the servlet.
- If the servlet is not yet instantiated, it will be instantiated and initialized by calling the init() method.
- The container calls public service() by passing ServletRequest and ServletResponse objects.
- Public service() method typecast ServletRequest and ServletResponse object to HttpServletRequest and HttpServletResponse objects respectively.
- Public service() method calls protected service().
- Protected service() method checks the client request & corresponding do___() method is called.
- The request is handled by sending the result generated by do___() to the client.
Below are some important advantages of the servlet as follows:
- Servlets are protocol-independent, i.e. it supports FTP, SMTP, etc. Mainly it provides extended support to HTTP protocol functionality.
- Servlets are persistent because it remains in memory until explicitly destroyed this helps in several request processing and one database connection can handle several database requests.
- Servlets are portable; since the servlets are written in java, they are portable and supports any web server.
- Faster in execution, servlets are compiled into byte code execute more quickly compared to other scripting languages. Byte code conversion gives better performance and helps in type checking and error.
Uses of Servlet Architecture
Let us see some of the uses of the servlet that are given below:
- Servlets are used to form data manipulation like accepting form data and generating dynamic HTML pages.
- Servlets helps in developing server load balancing applications where load balancing is among different servers.
- Servlets are used as the middle tier in enterprise network platforms for connecting the SQL database.
- Servlets can be integrated with applets to provide high-level interactivity and dynamic web content generation.
- Servlet is used to develop applications where they act as active agents in the middle tier, where they share data.
- Since the servlet supports various protocols like HTTP, FTF, etc., this helps develop applications like file server applications and chat enabled applications.
Compared to any other scripting languages, Java servlets are better in performance, and they are platform-independent. Servlets are dynamic in request and response processing. Since servlets support various protocols, hence servlets can be used while developing a web application to work with different protocols. Overall, servlets are the best fit in developing dynamic web applications.
This is a guide to Servlet Architecture. Here we discuss components, request flow, advantages, and uses of Servlet Architecture. You may also look at the following articles to learn more-