First page Back Continue Last page Graphics

What Are Servlets?


What Are Servlets?

Servlet technology is the foundation of Web application programming in Java. A servlet is a server-side Java program that gives Java-enabled servers additional functionality via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. Servlets may perform back-end functionality or can dynamically construct user interfaces.

The life cycle of a servlet is controlled by the container in which the servlet is deployed. When a request is mapped to a servlet, the container performs the following steps:

It loads the servlet class if there is no instance of it, creates an instance of the servlet class, and initializes the servlet instance by calling the init method.

It invokes the service method, passing a request and response object.

If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method.

When the browser request is for a servlet, the Web server forwards the request to the servlet container. The servlet dynamically constructs the HTML file, using any parameters that are sent in the URL request, and then the Web server returns the HTML file to the client browser.