Jakarta Servlet Explained

Jakarta Servlet
Author:Pavni Diwanji
Developer:Eclipse Foundation
Ver Layout:simple
Latest Release Version:6.0
Programming Language:Java
Platform:Jakarta EE
Size:2.56 MB
Genre:Software component for Web API
License:Eclipse Public License

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,[1] 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".[2] 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:

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 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 (Tomcat 7.0), 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 (also known as a servlet 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 (JSP) 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 (as shown in the example below) has become rare, the higher level MVC web framework in Jakarta EE (Faces) 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.[3] [4] 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 (JWS; then known by its codename Jeeves)[5] which would eventually be shipped as a product on June 5, 1997.[6]

In his blog on java.net, Sun veteran and GlassFish lead Jim Driscoll details the history of servlet technology.[7] 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.[8] [9] [10] This was before what is now the Jakarta EE was made into a specification.

The Servlet1 specification was created by Pavni Diwanji[11] [12] 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 history! Servlet API version !! Released !! Specification !! Platform !! Important Changes
Jakarta Servlet 6.0 May 31, 2022 6.0 Jakarta EE 10 remove deprecated features and implement requested enhancements
Jakarta Servlet 5.0 Oct 9, 2020 5.0 Jakarta EE 9 API moved from package to
Jakarta Servlet 4.0.3 Sep 10, 2019 4.0 Jakarta EE 8 Renamed from "Java" trademark
Java Servlet 4.0 Sep 2017 JSR 369 Java EE 8 HTTP/2
Java Servlet 3.1 May 2013 JSR 340 Java EE 7 Non-blocking I/O, HTTP protocol upgrade mechanism (WebSocket)[13]
Java Servlet 3.0 December 2009 JSR 315 Java EE 6, Java SE 6 Pluggability, Ease of development, Async Servlet, Security, File Uploading
Java Servlet 2.5 September 2005 JSR 154 Java EE 5, Java SE 5 Requires Java SE 5, supports annotation
Java Servlet 2.4 November 2003 JSR 154 J2EE 1.4, J2SE 1.3 web.xml uses XML Schema
Java Servlet 2.3 August 2001JSR 53 J2EE 1.3, J2SE 1.2 Addition of Filter
Java Servlet 2.2 August 1999JSR 902, JSR 903 J2EE 1.2, J2SE 1.2 Becomes part of J2EE, introduced independent web applications in .war files
Java Servlet 2.1 November 1998 2.1a 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[14]
Java Servlet 1.0 December 1996Part of June 1997 Java Servlet Development Kit (JSDK) 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.
    • The browser then generates an HTTP request for this URL.
    • This request is then sent to the appropriate server.
  2. 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.
  3. The container invokes the init method 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.
  4. The container invokes the service method 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.
  5. The servlet remains in the container's address space and is available to process any other HTTP requests received from clients.
    • The service method is called for each HTTP request.
  6. 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.
  7. 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.
  8. 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 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

Container servers

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

There are also other types of servlet containers such as those for SIP servlets, e.g., SailFin.

See also

References

External links

Notes and References

  1. Web site: Servlet (Java(TM) EE 7 Specification APIs). oracle.com. 2018-07-25.
  2. Web site: Servlet Essentials - Chapter 1 . novocode.com . https://web.archive.org/web/20171218173656/http://www.novocode.com/doc/servlet-essentials/chapter1.html . 2017-12-18.
  3. News: Freedman . Matt . 1996-06-26 . mdy . JavaOne conference report . . 2018-07-25 . 2018-07-26 . https://web.archive.org/web/20180726071907/https://www.javaworld.com/article/2077172/java-se/javaone-conference-report.html . dead .
  4. Diwanji . Pavani . Connelly . Dave . Wagle . Prasad . 1996-05-29 . mdy . Java Server and Servlets . Servers and Server Extensions . JavaOne 1996 . http://java.sun.com:80/javaone/javaone96/pres/ServExt.pdf . https://web.archive.org/web/20000816161328/http://java.sun.com/javaone/javaone96/pres/ServExt.pdf . 2000-08-16 . 2020-02-01 . live .
  5. News: Chang . Phil Inje . 1997-07-01 . mdy . Interview: The Java Web Server team gives you the skinny . . 2018-07-25 . 2018-07-26 . https://web.archive.org/web/20180726071912/https://www.javaworld.com/article/2076980/interview--the-java-web-server-team-gives-you-the-skinny.html . dead .
  6. News: Chang . Phil Inje . 1997-06-01 . mdy . Java Web Server ships! . . 2018-07-25 . 2018-07-26 . https://web.archive.org/web/20180726071909/https://www.javaworld.com/article/2076965/java-web-server-ships-.html . dead .
  7. Web site: Servlet History | community.oracle.com. 2005-12-10. Weblogs.java.net. 2013-06-14. 2020-08-15. https://web.archive.org/web/20200815055946/https://community.oracle.com/blogs/driscoll/2005/12/10/servlet-history. dead.
  8. Hunter . Jason . March 2000 . Servlet Timeline . Beyond Java Servlet Programming . O'Reilly Conference on Java . ftp://ftp.ora.com/pub/conference/java/Hunter_J/orabeyond.ppt . .
  9. Web site: Java Web Server . https://web.archive.org/web/19980111053338/http://jserv.javasoft.com/products/java-server/webserver/index.html . 1998-01-11 . Javasoft . . 2020-02-01 . live .
  10. Web site: Java Web Server(tm) . https://web.archive.org/web/20020206000714/http://sun.com/software/jwebserver/index.html . 2002-02-06 . . 2020-02-01 . live .
  11. Web site: Pavni Diwanji. Family Online Safety Institute. 12 November 2016. 26 July 2018. https://web.archive.org/web/20180726071809/https://www.fosi.org/people/pavni-diwanji/. dead.
  12. US . 5928323 . patent . Apparatus and method for dynamically generating information with server-side software objects . 1999-07-27 . 1999-07-27 . 1997-03-28 . 1996-05-30 . Gosling . James A. . James Gosling . Diwanji . Pavni . Connelly . David W. . .
  13. Web site: What's new in Servlet 3.1 ? - Java EE 7 moving forward (Arun Gupta, Miles to go ...). oracle.com. 22 November 2016.
  14. Book: Crawford . William . Hunter . Jason . November 1998 . Preface . Java Servlet Programming . 1st . . ix–x . 978-1-56592-391-1 . We cover Version 2.0 of the Servlet API, which was introduced as part of the Java Web Server 1.1 in December 1997 and clarified by the release of the Java Servlet Development Kit 2.0 in April 1998. . registration .