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.

Online Training Tutorials

  • servlets tutorialHttpServlet Class in ServletHttpServlet Class is a GenericServlet extension that adds methods for dealing with HTTP-specific data. To handle specific sorts of HTTP requests (GET, POST, and so on), HttpServlet […]
  • servlets tutorialServlet Database AccessServlet Database Access : Data that is shared between Web components and is persistent between invocations of a J2EE application is usually maintained by a database. Web components use the […]
  • servlets tutorialServlet Filter and Servlet ChainServlet Filter and Servlet Chain -We've looked at servlets that take requests from the server and provide results to the client. Servlets, on the other hand, were created as a generic […]
  • servlets tutorialServletRequest InterfaceServletRequest: When a servlet is asked to handle a request (ServletRequest), it often requires specific information about the request in order to process it effectively. The servlet will […]
  • servlets tutorialServlets Interview Questions and Answerswe have list out latest and updated Servlets interview questions and their answers for fresher’s as well as experienced users.  These interview question covers latest version of Servlets. […]
  • servlets tutorialServlets Tutorial – Java – TechnosapServlets Tutorial provides the basic and advanced concepts of Servlets. Our Servlets Tutorial is designed for beginners and professionals to learn Servlets online; Java servlets are key […]
  • GST’S Impact in SAP? Before Implementing SAP, Essential Things Need to be KnownGST’S Impact in SAP? Before Implementing SAP, Essential Things Need to be KnownEveryone want's to know impact of GST on SAP Modules, before implementing SAP with some basic knowledge, we herewith discuss GST impact in SAP with easy to understandable way. Impact of […]
  • sap checklistSAP Checklist for Full ClosingDetail Lit of SAP Checklist 1 Execute Report for Inter Company Activity & Journal Entries 2 Open posting period for next yr (T.Code: OB52) 3 Run Business Area's Assignment report. […]