Jakarta Servlet
| Company type | Software component |
|---|---|
| Industry | Software, Web development |
| Founded | May 1996 |
| Products | Java Servlet API |
A Jakarta Servlet, previously known as Java Servlet, is a Java software component designed to enhance server functionality. It primarily operates within web containers to host web applications on web servers, functioning as a server-side servlet web API. This technology serves as Java’s counterpart to other dynamic web content frameworks such as PHP and ASP.NET.
Introduction
A Jakarta Servlet represents a Java class adhering to the Jakarta Servlet API within the Jakarta EE platform, establishing a standardized interface for Java classes that manage client requests. Although servlets can support multiple client–server protocols, they are mainly utilized over HTTP. The majority of servlets extend the class, causing the term "servlet" to be commonly used as shorthand for "HTTP servlet." Servlets are responsible for producing dynamic web output, typically formatted in HTML, XML, or JSON.
The Jakarta Servlet API has been partially replaced by:
- Jakarta RESTful Web Services (JAX-RS 2.0), which caters to AJAX, JSON, and RESTful services,
- Jakarta XML Web Services (JAX-WS), which supports SOAP-based web services.
A is an entity that processes incoming requests and formulates responses. The API defines objects that encapsulate requests, responses, configuration details, and runtime contexts.
The package offers HTTP-specific extensions of GenericServlet along with session management utilities that monitor multiple client interactions.
To preserve state across multiple transactions, servlets use session variables facilitated through HTTP cookies or URL rewriting. Prior to Servlet 3.0 (Tomcat 7.0), URL mappings were configured via web.xml. Starting with Servlet 3.0, the @WebServlet annotation enables declarative mapping of servlets to URL patterns.
Servlets are generally bundled within a WAR file as components of a web application.
Deployment necessitates a web container (or servlet container) responsible for managing the lifecycle of servlets, mapping URLs, and controlling access permissions.
Servlets may also be automatically generated by compiling Jakarta Server Pages (JSP). Unlike servlets that embed HTML code within Java classes, JSP inserts Java snippets into HTML markup. Best practices advocate separating business logic into servlets while delegating presentation responsibilities to JSPs, thereby adhering to the single-responsibility principle.
Although direct generation of HTML through servlets has diminished in popularity, Jakarta EE’s MVC framework (Jakarta Faces) continues to rely on servlet technology via the for handling request and response cycles.
A common architectural approach combines servlets and JSPs into what is known as "JSP model 2 architecture", which is an implementation of the model–view–controller pattern.
History
The Java Servlet API debuted at the first-ever JavaOne conference held in May 1996. Approximately two months later, an initial public alpha version was released on the JavaSoft website as part of the Java Web Server (JWS; code-named Jeeves), which officially launched on June 5, 1997.
Jim Driscoll, a Sun Microsystems veteran and lead on GlassFish, recounted the servlet technology’s origins, highlighting that James Gosling conceived servlets early during Java’s inception. However, it was not until December 1996 with JWS that it became a tangible product—preceding the formal establishment of what is now known as Jakarta EE.
The Servlet 1.0 specification was authored by Pavni Diwanji at Sun Microsystems, reaching completion in June 1997. From version 2.2 onward, development of the specification has been governed by the Java Community Process.
| Servlet API version | Release Date | Specification | Platform | Key Enhancements |
|---|---|---|---|---|
| Jakarta Servlet 6.0 | May 31, 2022 | 6.0 | Jakarta EE 10 | Removed deprecated elements and introduced improvements |
| Jakarta Servlet 5.0 | Oct 9, 2020 | 5.0 | Jakarta EE 9 | Transitioned package from <syntaxhighlight lang="text" class="" style="" inline="1">javax.servlet</syntaxhighlight> to <syntaxhighlight lang="text" class="" style="" inline="1">jakarta.servlet</syntaxhighlight> |
| Jakarta Servlet 4.0.3 | Sep 10, 2019 | 4.0 | Jakarta EE 8 | Rebranded from "Java" trademark |
| Java Servlet 4.0 | Sep 2017 | JSR 369 | Java EE 8 | Added support for HTTP/2 |
| Java Servlet 3.1 | May 2013 | JSR 340 | Java EE 7 | Introduced non-blocking I/O and HTTP upgrade capabilities for WebSocket |
| Java Servlet 3.0 | Dec 2009 | JSR 315 | Java EE 6 | Added pluggability features, asynchronous processing, enhanced security, and file upload support |
| Java Servlet 2.5 | Sep 2005 | JSR 154 | Java EE 5 | Required Java SE 5; introduced annotation support |
| Java Servlet 2.4 | Nov 2003 | JSR 154 | J2EE 1.4 | Adopted XML Schema for web.xml
|
| Java Servlet 2.3 | Aug 2001 | JSR 53 | J2EE 1.3 | Added Filter interface
|
| Java Servlet 2.2 | Aug 1999 | JSR 902, JSR 903 | J2EE 1.2 | Incorporated into J2EE; enabled deployment of independent web applications packaged as .war files |
| Java Servlet 2.1 | Nov 1998 | 2.1a | Unspecified platform | First official specification; introduced RequestDispatcher and ServletContext
|
| Java Servlet 2.0 | Dec 1997 | n/a | JDK 1.1 | Released alongside April 1998’s Java Servlet Development Kit (JSDK) version 2.0 |
| Java Servlet 1.0 | Dec 1996 | n/a | N/A | Included in June 1997’s JSDK version 1.0 |
Life cycle of a servlet
The servlet life cycle is primarily governed by three key methods: init(), service(), and destroy(). Every servlet implements these methods, which the server invokes at designated stages.
- When a servlet is initialized, the web container executes the
init()method, supplying a configuration object that implements thejavax.servlet.ServletConfiginterface. This grants the servlet access to initialization parameters defined as name-value pairs within the web application. - Following initialization, each incoming client request is processed in a separate thread. For every request, the container calls the servlet’s
service()method. This method identifies the type of request and forwards it to the corresponding handler implemented by the developer. If the requested handler method is absent, the superclass method is invoked, typically resulting in an error. - When the servlet is removed from service, the container calls the
destroy()method once to free up any resources held by the servlet.
A common user interaction flow:
- A user issues a URL request, which generates an HTTP request directed to the server.
- The web server passes this request to the servlet container.
- The container locates and loads the appropriate servlet into memory.
- It then calls
init(), providing any initialization parameters if present. - The container invokes
service()to handle the HTTP request and produce a response. - The servlet remains resident in memory, ready to process additional requests via repeated calls to
service(). - Eventually, when unloading the servlet, the container triggers
destroy()to release resources and persist necessary data. - Finally, memory allocated to the servlet is reclaimed through garbage collection.
Example
The following example servlet demonstrates counting how many times its service() method has been executed.
HttpServlet inherits from GenericServlet, thereby implementing the Servlet interface.
Typically, HttpServlet's service() delegates requests to specific methods such as doGet(), doPost(), etc., based on HTTP request types. In this example, however, service() is overridden without differentiating between HTTP methods.
<syntaxhighlight lang="java">
import java.io.IOException;
import jakarta.servlet.ServletConfig; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
public class ServletLifeCycleExample extends HttpServlet {
private Integer sharedCounter;
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
getServletContext().log("init() called");
sharedCounter = 0;
}
@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
getServletContext().log("service() called");
int localCounter;
synchronized (sharedCounter) {
sharedCounter++;
localCounter = sharedCounter;
}
response.getWriter().write("Incrementing the count to " + localCounter);
response.getWriter().flush();
}
@Override
public void destroy() {
getServletContext().log("destroy() called");
}
} </syntaxhighlight>
Container servers
Servlet technology is incorporated into a wide range of products; for a comprehensive list of implementations, refer to the web container page.
There are also alternative servlet container types, including SIP servlet containers such as SailFin.
See also
- Apache JServ Protocol (AJP)
References
- Murach, Joel and Michael Urban. Murach's Java Servlets and JSP. 2014.
Tutorials
- How to Start With Servlets Using Jakarta EE 10 | Jakarta EE | The Eclipse Foundation
- Jakarta Servlet, Jakarta Faces and Jakarta Server Pages Explained | Jakarta EE | The Eclipse Foundation
- Jakarta Servlet :: Jakarta EE Tutorial :: Jakarta EE Documentation
External links
- Servlets.com (archived)