Methods of Servlet

Servlets are server side components and it’s a powerful mechanism for developing the server-side programs. Servlets is a component-based, the platform-independent methods for building Web-based applications without the performance limitations of CGI programs. By using servlets web developers can make server-side applications quickly and efficiently it can run on any servlet-enabled Web server. Servlets  can run  inside the Java virtual machine. At  the time of  servlet is running on the server side so that the browser is not checked for compatibility. Servlets can access the entire family of Java APIs, including JDBC API to access enterprise databases. Servlets can also access a library of HTTP-specific calls, receive all the benefits of the maturity of the Java language, including portability, performance, reusability, and crash protection. Servlets HTTP protocols are used in several java classes in the javax.servlet packages and javax.servlet.http.

Uses of HTTP Servlet:

  1. Providing dynamic content, such as obtaining the results of a database query and return to the client.
  2. Process and / or storing data submitted by the HTML.
  3. Manage information on the status of a stateless HTTP server.

For example: – A car online shopping application manages multiple clients simultaneously.

Methods of  servlet

Servlet consists of five methods:

1) Init ()

The init () method is called only once throughout the life of a servlets and this init () method the servlet has been placed into service. The servlet cannot be put into the service if,

A) The init () method does not return within a fix time set by the web server.

B) It throws a ServletException.

Syntax: – public void init (ServletConfig config) throws ServletException.

Parameters- The init () method takes a ServletConfig object containing initialization parameters and servlet’s configuration and throws a ServletException if an exception has occurred.

2) Service ()

Once the servlet starts receiving request, the service () method is called by the servlet container to respond. The servlet services the client request with the help of two objects,javax.servlet.ServletRequest and javax.servlet.ServletResponse are passed by the Servlet container.

Syntax: – public void service (ServletRequest req, ServletResponse res) throws ServletException.

Parameters: – The service () method takes the ServletRequest object that contains the client’s request and the object ServletResponse contains the servlet’s response.

3) getServletConfig ()

This method contains parameters for initialization and startup of the servlet and returns a ServletConfig object.

Syntax: – public ServletConfig getServletConfig ()

4) getServletInfo ()

The getServletInfo () method is returned by version, author etc.

Syntax: – public String getServletInfo ()

5) destroy ()

This method is used to close the servlet.

Syntax: – public void destroy ()

 

Methods of Servlet