servlets tutorial

Servlet Life Cycle

Servlet Life Cycle, When a client requests a servlet, the server loads and runs the necessary Java classes. use those classes to build content, which the server then feeds back to the client. In most cases, the client is a web browser, the server is a web server, and the servlet returns ordinary HTML. From the standpoint of a web browser, this is no different than requesting a page generated by a CGI script or normal HTML. On the server side, however, there is a significant difference: persistence. Instead of shutting down at the end of each request, the servlet can remain loaded and ready to handle subsequent requests.

A servlet’s request-processing time can vary, although it is often relatively fast when compared to a comparable CGI programme. The fundamental performance advantage of a servlet is that the majority of the setup expense is incurred just once. The init() method of a servlet is called when it is loaded. You can use init() to build I/O-intensive resources for usage across several invocations, such as database connections.

Servlet Life Cycle

If you have a high-traffic site, the performance benefits can be quite dramatic. Instead of putting up and tearing down a hundred thousand database connections, the servlet just needs to create a connection once. After the init() method runs, the servlet container marks the servlet as available.

To process each incoming connection directed at a specific servlet, the container invokes the servlet’s serve() function. All resources produced by the init() method are available to the service() method. When the server goes down, the destroy() method of the servlet is called to clean up the resources.

Most common CGI jobs need a significant amount of fussing on the side of the programmer; even decoding HTML form arguments can be difficult, let alone dealing with cookies and session tracking. Libraries are available to assist with these activities, but they are not free.

Writing Servlets — How it Works?

There are 3 core elements of the Servlet API are the javax.servlet.Servlet interface, javax.servlet.GenericServlet class, and the javax.servlet.http.

HttpServlet class. Normally, you create a servlet by subclassing one of the two classes, although if you are adding servlet capability to an existing object, you may find it easier to implement the interface.

The GenericServlet class is used for servlets that don’t implement any particular communication protocol. Here’s a basic servlet that demonstrates servlet structure by printing a short message:

import javax.servlet.*;
import java.io.*;
public class BasicServlet extends GenericServlet {
public void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
// We won't use the ServletRequest object in this example
out.println("Hello.");
}
}

BasicServlet extends the GenericServlet class and implements one method: service(). Whenever a server wants to use the servlet, it calls the service() method, passing ServletRequest and ServletResponse objects (we’ll look at these in more detail shortly).


The GenericServlet class can also implement a filtering servlet that takes output from an unspecified source and performs some kind of alteration.

Although most servlets today work with web servers, GenericServlet does not require this; the class implements only a generic servlet. The HttpServlet class, as we’ll see in a moment, is a subclass of GenericServlet that is built to operate with the HTTP protocol.