Jakarta Servlet


A Jakarta Servlet is a Java software component that extends the capabilities of a server. Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web applications on web servers and thus qualify as a server-side servlet web API. Such web servlets are the Java counterpart to other dynamic web content technologies such as PHP and ASP.NET.

Introduction

A Jakarta Servlet processes or stores a Java class in Jakarta EE that conforms to the Jakarta Servlet API, a standard for implementing Java classes that respond to requests. Servlets could in principle communicate over any client–server protocol, but they are most often used with HTTP. Thus "servlet" is often used as shorthand for "HTTP servlet". Thus, a software developer may use a servlet to add dynamic content to a web server using the Java platform. The generated content is commonly HTML, but may be other data such as XML and more commonly, JSON. Servlets can maintain state in session variables across many server transactions by using HTTP cookies, or URL mapping.
The Jakarta Servlet API has, to some extent, been superseded by two standard Java technologies for web services:
To deploy and run a servlet, a web container must be used. A web container is essentially the component of a web server that interacts with the servlets. The web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights.
The Servlet API, contained in the Java package hierarchy, defines the expected interactions of the web container and a servlet.
A is an object that receives a request and generates a response based on that request. The basic Servlet package defines Java objects to represent servlet requests and responses, as well as objects to reflect the servlet's configuration parameters and execution environment. The package defines HTTP-specific subclasses of the generic servlet elements, including session management objects that track multiple requests and responses between the web server and a client. Servlets may be packaged in a WAR file as a web application.
Servlets can be generated automatically from Jakarta Server Pages by the Jakarta Server Pages compiler. The difference between servlets and JSP is that servlets typically embed HTML inside Java code, while JSPs embed Java code in HTML. While the direct usage of servlets to generate HTML has become rare, the higher level MVC web framework in Jakarta EE still explicitly uses the servlet technology for the low level request/response handling via the. A somewhat older usage is to use servlets in conjunction with JSPs in a pattern called "Model 2", which is a flavor of the model–view–controller.
The current version of Servlet is 4.0.

History

The Java Servlet API was first publicly announced at the inaugural JavaOne conference in May 1996. About two months after the announcements at the conference, the first public implementation was made available on the JavaSoft website. This was the first alpha of the Java Web Server which would eventually be shipped as a product on June 5, 1997.
In his blog on java.net, Sun veteran and GlassFish lead Jim Driscoll details the history of servlet technology. James Gosling first thought of servlets in the early days of Java, but the concept did not become a product until December 1996 when Sun shipped JWS. This was before what is now the Jakarta EE was made into a specification.
The Servlet1 specification was created by Pavni Diwanji while she worked at Sun Microsystems, with version 1.0 finalized in June 1997. Starting with version 2.2, the specification was developed under the Java Community Process.
Servlet API versionReleasedSpecificationPlatformImportant Changes
Jakarta Servlet 5.0.0 M1Jun 12, 2020Jakarta EE 9API moved from package to
Jakarta Servlet 4.0.3Aug 13, 2019Jakarta EE 8Renamed from "Java" trademark
Java Servlet 4.0Sep 2017Java EE 8HTTP/2
Java Servlet 3.1May 2013Java EE 7Non-blocking I/O, HTTP protocol upgrade mechanism
Java Servlet 3.0Java EE 6, Java SE 6Pluggability, Ease of development, Async Servlet, Security, File Uploading
Java Servlet 2.5JSR 154Java EE 5, Java SE 5Requires Java SE 5, supports annotation
Java Servlet 2.4J2EE 1.4, J2SE 1.3web.xml uses XML Schema
Java Servlet 2.3J2EE 1.3, J2SE 1.2Addition of Filter
Java Servlet 2.2, J2EE 1.2, J2SE 1.2Becomes part of J2EE, introduced independent web applications in.war files
Java Servlet 2.1UnspecifiedFirst official specification, added RequestDispatcher, ServletContext
Java Servlet 2.0December 1997JDK 1.1Part of April 1998 Java Servlet Development Kit 2.0
Java Servlet 1.0December 1996Part of June 1997 Java Servlet Development Kit 1.0

Life cycle of a servlet

Three methods are central to the life cycle of a servlet. These are init, service, and destroy.
They are implemented by every servlet and are invoked at specific times by the server.
The following is a typical user scenario of these methods.
  1. Assume that a user requests to visit a URL.
  2. * The browser then generates an HTTP request for this URL.
  3. * This request is then sent to the appropriate server.
  4. The HTTP request is received by the web server and forwarded to the servlet container.
  5. * The container maps this request to a particular servlet.
  6. * The servlet is dynamically retrieved and loaded into the address space of the container.
  7. The container invokes the init method of the servlet.
  8. * This method is invoked only when the servlet is first loaded into memory.
  9. * It is possible to pass initialization parameters to the servlet so that it may configure itself.
  10. The container invokes the service method of the servlet.
  11. * This method is called to process the HTTP request.
  12. * The servlet may read data that has been provided in the HTTP request.
  13. * The servlet may also formulate an HTTP response for the client.
  14. The servlet remains in the container's address space and is available to process any other HTTP requests received from clients.
  15. * The service method is called for each HTTP request.
  16. The container may, at some point, decide to unload the servlet from its memory.
  17. * The algorithms by which this decision is made are specific to each container.
  18. The container calls the servlet's destroy method to relinquish any resources such as file handles that are allocated for the servlet; important data may be saved to a persistent store.
  19. The memory allocated for the servlet and its objects can then be garbage collected.

    Example

The following example servlet prints how many times its service method was called.
Note that HttpServlet is a subclass of GenericServlet, an implementation of the Servlet interface.
The service method of HttpServlet class dispatches requests to the methods doGet, doPost, doPut, doDelete, and so on; according to the HTTP request. In the example below service is overridden and does not distinguish which HTTP request method it serves.

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletLifeCycleExample extends HttpServlet

Container servers

The specification for Servlet technology has been implemented in many products. See a list of implementations on the web container page.