HOME

TheInfoList



OR:

A Jakarta Servlet (formerly Java Servlet) is a
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
software component that extends the capabilities of a
server Server may refer to: Computing *Server (computing), a computer program or a device that provides functionality for other programs or devices, called clients Role * Waiting staff, those who work at a restaurant or a bar attending customers and su ...
. Although servlets can respond to many types of requests, they most commonly implement web containers for hosting
web application A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection. History In earlier computing models like client-serv ...
s on
web server A web server is computer software and underlying hardware that accepts requests via HTTP (the network protocol created to distribute web content) or its secure variant HTTPS. A user agent, commonly a web browser or web crawler, initia ...
s and thus qualify as a server-side servlet
web API A web API is an application programming interface for either a web server or a web browser. It is a web development concept, usually limited to a web application's client-side (including any web frameworks being used), and thus usually does not ...
. Such web servlets are the
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
counterpart to other
dynamic web content A server-side dynamic web page is a web page whose construction is controlled by an application server processing server-side scripts. In server-side scripting, parameters determine how the assembly of every new web page proceeds, and includin ...
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 The Hypertext Transfer Protocol (HTTP) is an application layer protocol in the Internet protocol suite model for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide We ...
. Thus "servlet" is often used as shorthand for "HTTP servlet". Thus, a
software developer Software development is the process of conceiving, specifying, designing, programming, documenting, testing, and bug fixing involved in creating and maintaining applications, frameworks, or other software components. Software development invo ...
may use a servlet to add dynamic content to a
web server A web server is computer software and underlying hardware that accepts requests via HTTP (the network protocol created to distribute web content) or its secure variant HTTPS. A user agent, commonly a web browser or web crawler, initia ...
using the Java platform. The generated content is commonly
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaS ...
, but may be other data such as
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. T ...
and more commonly,
JSON JSON (JavaScript Object Notation, pronounced ; also ) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other se ...
. Servlets can maintain state in session variables across many server transactions by using
HTTP cookie HTTP cookies (also called web cookies, Internet cookies, browser cookies, or simply cookies) are small blocks of data created by a web server while a user is browsing a website and placed on the user's computer or other device by the user's ...
s, or URL mapping. The Jakarta Servlet API has, to some extent, been superseded by two standard Java technologies for web services: * the Jakarta RESTful Web Services (JAX-RS 2.0) useful for AJAX, JSON and REST services, and * the Jakarta XML Web Services (JAX-WS) useful for
SOAP Soap is a salt of a fatty acid used in a variety of cleansing and lubricating products. In a domestic setting, soaps are surfactants usually used for washing, bathing, and other types of housekeeping. In industrial settings, soaps are us ...
Web Services. To deploy and run a servlet, a web container must be used. 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. The Servlet API, contained in the Java package hierarchy , defines the expected interactions of the web container and a servlet. A is an
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
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 The Hypertext Transfer Protocol (HTTP) is an application layer protocol in the Internet protocol suite model for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide We ...
-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 A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection. History In earlier computing models like client-serv ...
. Servlets can be generated automatically from
Jakarta Server Pages Jakarta Server Pages (JSP; formerly JavaServer Pages) is a collection of technologies that helps software developers create dynamically generated web pages based on HTML, XML, SOAP, or other document types. Released in 1999 by Sun Microsystems, ...
(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. 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 ( JSF) 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 Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements. This is done to separate internal representations of info ...
.


History

The Java Servlet API was first publicly announced at the inaugural
JavaOne __NOTOC__ JavaOne is an annual conference first organized in 1996 by Sun Microsystems to discuss Java technologies, primarily among Java developers. It was held in San Francisco, California, typically running from a Monday to Thursday in summer ...
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 (JWS; then known by its codename ''Jeeves'') 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 Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, 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 Sun Microsystems, Inc. (Sun for short) was an American technology company that sold computers, computer components, software, and information technology services and created the Java programming language, the Solaris operating system, ZFS, t ...
, with version 1.0 finalized in June 1997. Starting with version 2.2, the specification was developed under the Java Community Process.


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. * During initialization stage of the servlet life cycle, the web container initializes the servlet instance by calling th
init()
method, passing an object implementing th

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 service() method of the servlet for every request. The service() method 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 destroy() method that takes the servlet out of service. The destroy() method, like init(), is called only once in the lifecycle of a servlet. The following is a typical user scenario of these methods. # 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 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. # 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. # 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. # 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 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. # 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 SailFin was an open-source Java application server project led by Sun Microsystems. It implements the JCP SIP Servlet 1.1 (JSR 289) specification integrated with the open-source Java EE application server GlassFish. SailFin effectively extends ...
.


References


External links

*
Servlets.com
{{Authority control Articles with example Java code Servlet Servlet