Jakarta Servlet
A Jakarta Servlet, formerly Java 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 is 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. In principle, any servlets can extend the class; however, realistically speaking, all servlets extend the class. Thus "servlet" is often used as shorthand for "HTTP servlet". Thus, a servlet can be used 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.The Jakarta Servlet API has, to some extent, been superseded by two standard Java technologies for web services:
- the Jakarta RESTful Web Services useful for AJAX, JSON and REST services, and
- the Jakarta XML Web Services useful for SOAP Web Services.
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 Servlet API, contained in the Java package hierarchy, defines the expected interactions of the web container and a servlet.
The package defines HTTP-specific subclasses of the
GenericServlet. This package includes session management objects that track multiple requests and responses between the web server and a client.Servlets can maintain state in session variables across many server transactions by using HTTP cookies, or URL mapping. There are several ways of creating a servlet and using URL mapping with a servlet. Before servlet 3.0 specification, configuring the web.xml to map a servlet to a URL was the only option. For applications using the servlet 3.0 specification or later, the
@WebServlet annotation can be used to map any servlet to one or more URL patterns.Servlets may be packaged in a WAR file as a web application.
A web container is required for deploying and running a servlet. 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.
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. In general, when using JSPs, embedding Java code in JSP is considered bad practice. Instead, a better approach would be to move the back-end logic from the JSP to the Java code in the
Servlet. This ensures that the Servlet is only responsible for processing, and the JSP is only responsible for presenting the HTML, allowing for a clear separation of concerns and conformance to the single-responsibility principle.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.
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 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 version | Released | Specification | Platform | Important Changes |
| Jakarta Servlet 6.1 | March 28, 2024 | Jakarta EE 11 | improve HTTP status code and character encoding support | |
| Jakarta Servlet 6.0 | May 31, 2022 | Jakarta EE 10 | remove deprecated features and implement requested enhancements | |
| Jakarta Servlet 5.0 | October 9, 2020 | Jakarta EE 9 | API moved from package to | |
| Jakarta Servlet 4.0.3 | September 10, 2019 | Jakarta EE 8 | Renamed from "Java" trademark | |
| Java Servlet 4.0 | September 2017 | Java EE 8 | HTTP/2 | |
| Java Servlet 3.1 | May 2013 | Java EE 7 | Non-blocking I/O, HTTP protocol upgrade mechanism | |
| Java Servlet 3.0 | December 2009 | Java EE 6 | Pluggability, Ease of development, Async Servlet, Security, File Uploading | |
| Java Servlet 2.5 | September 2005 | JSR 154 | Java EE 5 | Requires Java SE 5, supports annotation |
| Java Servlet 2.4 | November 2003 | J2EE 1.4 | web.xml uses XML Schema | |
| Java Servlet 2.3 | August 2001 | J2EE 1.3 | Addition of Filter | |
| Java Servlet 2.2 | August 1999 | , | J2EE 1.2 | Becomes part of J2EE, introduced independent web applications in.war files |
| Java Servlet 2.1 | November 1998 | Unspecified | First official specification, added RequestDispatcher, ServletContext | |
| Java Servlet 2.0 | December 1997 | JDK 1.1 | Part of April 1998 Java Servlet Development Kit 2.0 | |
| Java Servlet 1.0 | December 1996 | Part 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 areinit, service, and destroy.They are implemented by every servlet and are invoked at specific times by the server.
- During initialization stage of the servlet life cycle, the web container initializes the servlet instance by calling the method, passing an object implementing the interface. This configuration object allows the servlet to access name-value initialization parameters from the web application.
- After initialization, the servlet instance can service client requests. Each request is serviced in its own separate thread. The web container calls the
servicemethod of the servlet for every request. Theservicemethod determines the kind of request being made and dispatches it to an appropriate method to handle the request. The developer of the servlet must provide an implementation for these methods. If a request is made for a method that is not implemented by the servlet, the method of the parent class is called, typically resulting in an error being returned to the requester. - Finally, the web container calls the
destroymethod that takes the servlet out of service. Thedestroymethod, likeinit, is called only once in the lifecycle of a servlet.
- Assume that a user requests to visit a URL.
- * The browser then generates an HTTP request for this URL.
- * This request is then sent to the appropriate server.
- The HTTP request is received by the web server and forwarded to the servlet container.
- * The container maps this request to a particular servlet.
- * The servlet is dynamically retrieved and loaded into the address space of the container.
- The container invokes the
initmethod of the servlet. - * This method is invoked only when the servlet is first loaded into memory.
- * It is possible to pass initialization parameters to the servlet so that it may configure itself.
- The container invokes the
servicemethod of the servlet. - * This method is called to process the HTTP request.
- * The servlet may read data that has been provided in the HTTP request.
- * The servlet may also formulate an HTTP response for the client.
- The servlet remains in the container's address space and is available to process any other HTTP requests received from clients.
- * The
servicemethod is called for each HTTP request. - The container may, at some point, decide to unload the servlet from its memory.
- * The algorithms by which this decision is made are specific to each container.
- The container calls the servlet's
destroymethod to relinquish any resources such as file handles that are allocated for the servlet; important data may be saved to a persistent store. - The memory allocated for the servlet and its objects can then be garbage collected.