What is Servlet in Java

Introduction of Servlet

Servlet in java is a generic server extension. It is a kind of java class which can be loaded dynamically to expand the functionality of a Server. Servlets are used with web servers and run inside a JVM on the server. Unlike applets, your browser need not have required support for java. It is like CGI (Common Gateway Interface) can handle request and response from or to the web server.

Servlets are portable and platform independent. Servlets are the component of the JEE framework used for web development. They are basically Java programs that run inside the container. They are responsible for accepting a request, processing it, and sending a response back.

Servlet need to be registered first in web configuration file so that a Servlet Container instantiates a servlet by calling its init() method  at the start-up.

Once its initialization is complete, the servlet is ready to accept incoming requests. Subsequently, the container directs these requests for processing in the servlet's service() method. After that, it further delegates the request to the appropriate method such as doGet() or doPost() based on the HTTP request type.

When web server stopped, using destroy(), the Servlet container destroy the servlet object, make the servlet down, and it can no longer accept incoming requests


Web Server

It is a program or software installed on a computer system which can interacts with the client through the web browser. It delivers web pages to the client and handle the events (request/response) between client and server using HTTP/HTTPS protocol. We can consider the web server as the package of a large number of programs installed on a computer system connected to network (internet/intranet) for getting request data/file, sending response, serving e-mail, publishing web pages, publish/consume api’s and establishing connection between other servers.

In Java, web server is a server that is used to support the web component like the web application build with Servlet and JSP. Web server having separate container for Servlet and JSP. An web server can be following category :-

1)      HTTP server ( to access web pages or services)

2)      SMTP server ( to support message or mail service)

3)      FTP server ( to transfer files on network )

4)      NNTP server (Network News Transfer Protocol)

An web server contains various Containers to compile and execute different types of server side programs. Web server supporting Java used “Servlet Container” and “JSP container” for process/execute Servlet or JSP programs

Servlet Container

Servlet container are set of compiled executable programs inside Web server which have pre-defined rules and capability to manage requests and responses using Servlet. These programs validate the requests and responses to ensure they meet the Java servlet specifications.

Servlet Container load, initialize and execute the Servlets. It handles large number of request as it can hold many active servlets and listeners. A Servlet Container my run standalone without web server.

JSP Container

JSP container are set of compiled executable programs inside Web server which have pre-defined rules and capability to compile the JSP file after deployment, manage requests and responses. These programs validate the requests and responses to ensure they meet the Java servlet specifications.

Lifecycle of a Servlet

Servlet need to be registered first in web configuration file so that a Servlet Container instantiates a servlet by calling its init() method  at the start-up.

Once its initialization is complete, the servlet is ready to accept incoming requests. Subsequently, the container directs these requests for processing in the servlet's service() method. After that, it further delegates the request to the appropriate method such as doGet() or doPost() based on the HTTP request type.

When web server stopped, using destroy(), the Servlet container destroy the servlet object, make the servlet down, and it can no longer accept incoming requests. This is called as life cycle of a Servlet in java. 



1)     Init method: Each servlet contains this method by default, it will execute only once when Servlet instantiate.

2)     Service method: Generic servlet contains this method. Usually this method contains the business logic to handle http request and http response.

3)    Destroy method: This method used by Servlet Container to kill the servlet object and release the memory. It will be called while web server stopped.


Comments

Popular Topics

What is JSP. How to create one JSP application in Eclipse

How to develop a RESTful webservice in java ?

Data Types in PL/SQL

What is JDBC ?

What is Hibernate in java ?

If you have any query or suggestions, Please feel free to write us

Name

Email *

Message *