Sunday, December 24, 2017

What is Servlet?

Servlet term is used in 2 different contexts with 2 different meaning.
In the broader context, it represents technology provided by SUN Microsystems for developing web application in java. It is web component that is deployed on the server to create dynamic web pages.
In the narrow context, it represent a class for web application component that is created using the technology and that is responsible for processing request on a web server.

Before Servlet web application were developed using CGI (Common Getaway Interface). CGI was a protocol that specifies how an application program communicates with the web server in standard manner. In CGI based web application program were written C, C++, Perl. These program uses to follow CGI protocol for interaction with web server.


CGI based application had following problems:
1-   CGI scripts were platform dependent.
Execution of CGI script was process based. Process based execution limits scalability due to limited hardware resources such as RAM, Processor etc..





Servlet as a technology removes both the problems associated with CGI. By providing platform independent and thread based request processing model.
As a technology servlet represents set of classes and interfaces provided by SUN Microsystems. These interfaces define the standard communication model between web server and servlets.
javax.servlet package contains classes and interface of servlet API.
At the core of servlet API is an interface named javax.servlet.Servlet which provides life cycle methods of servlet. It need to implemented.


Life cycle methods of Servlet interface:
1-init():  
It is used by the web developer to perform initialization operation. This method is invokes only once just after Servlet object is created. It is used by the server to provide the reference of a ServletConfig object to the servlet.
ServletConfig is an interface of servlet API, implementation of which is provided by web server vendors.
public void init(ServletConfig config);

2- service(): This is a request processing method. This method is invoked each time a request for the Servlet is received.It is used by the Servlet for processing the request.
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException;

In the service() method object of type ServletRequest and ServletResponse are provided by the web server as parameter.
ServletRequest and ServletResponse are interface of Servlet API. Implementation of these provided by web server vendors. These interfaces defines a standard mode of communication between Servlet and server.




1.1- ServletRequest object is created and request data is stored in it.
1.2- ServletResponse object is created to receive response from the Servlet.
1.3- service() method is invoked and references of ServletRequest and  
       ServletResponse objects are provided.
1.4- Request data is read form the ServletRequest object by the Servlet for
       processing.
1.5- Processing logic is applied and result is store in ServletResponse object.
1.6- After the completion of service() method server reads the contents of
       ServletResponse object .
1.7- Response is sent to the client.

3- destroy(): This method is invoked only one just before Servlet is unloaded. It is used by the application developers to perform cleanup operations.
    public void destroy();

Non Life cycle methods of Servlet interface:
1- getServletConfig() :
                                    This methos is used to obtain the reference of the  
    ServletConfig object from a Servlet object.
    public ServletConfig getServletConfig();

2- getServletInfo() :
                                  
This method is used by the application developer to
    describes there servlets.
    public String getServletInfo();

Note: web Server create object for a requested Servlet, so there is need to predefine method (Life cycle methods) that is call by web server to communicate with Servlet and provides services

No comments:

Post a Comment

Java Functional Interface

Java Lambda expression Lambda expression came to enable to use functional programming feature in java. It provides the facility t...