Java Servlet
Java Servlet
1. Read the explicit data sent by the clients (browsers). This includes an
The Java Servlet, now also known as the Jakarta Servlet, is a Java Server HTML form on a Web page or it could also come from an applet or a
Software component, designed and deployed to enhance the Server custom HTTP client program.
services by upgrading their capabilities to respond to any requests through 2. Read the implicit HTTP request data sent by the clients (browsers).
a Web API. This includes cookies, media types and compression schemes the
Java Servlet or Jakarta Servlet as the technology to design and deploy browser understands, and so forth.
dynamic web pages using the Java Programming Language. It implements 3. Process the data and generate the results. This process may require
a typical servlet in the client-server architecture, and the Servlet lives on talking to a database, executing an RMI or CORBA call, invoking a
the server-side. Web service, or computing the response directly.
4. Send the explicit data (i.e., the document) to the clients (browsers).
Servlets provide a component-based, platform-independent method for This document can be sent in a variety of formats, including text
building Web based applications, without the performance limitations of (HTML or XML), binary (GIF images), Excel, etc.
CGI programs. Servlets have access to the entire family of Java APIs, 5. Send the implicit HTTP response to the clients (browsers). This
including the JDBC API to access enterprise databases. includes telling the browsers or other clients what type of document is
Java Servlets often serve the same purpose as programs implemented using being returned (e.g., HTML), setting cookies and caching parameters,
the Common Gateway Interface (CGI). But Servlets offer several and other such tasks.
advantages in comparison with the CGI.
1. Performance is significantly better. There are several varieties of interfaces and classes available
in the Servlet API. Some of them are as follows:
2. Servlets execute within the address space of a Web server. It is not
HTTP Servlet
necessary to create a separate process to handle each client request.
Generic Servlet
3. Servlets are platform-independent because they are written in Java. Servlet Request
Servlet Response
4. Java security manager on the server enforces a set of restrictions to
protect the resources on a server machine. So servlets are trusted.
5. The full functionality of the Java class libraries is available to a
servlet. It can communicate with applets, databases, or other software
via the sockets and RMI mechanisms that you have seen already.
SERVLET TYPES-
• Generic Servlets: These are those servlets that Every servlet should override the following 3 methods namely:
provide functionality for implementing a 1.init(): To initalize/instantiate the servlet container.
servlet. It is a generic class from which all the 2.service(): This method acts like an intermediatory between the HTTP request and the business
customizable servlets are derived. It is logic to serve that particular request.
protocol-independent and provides support for 3.destroy(): This method is used to deallocate the memory allocated to the servlet.
HTTP, FTP, and SMTP protocols. The class
used is ‘javax.servlet.Servlet’ and it only has 2 These methods are used to process the request from the user.
methods – init() to initialize & allocate memory
to the servlet and destroy() to deallocate the JAVA SERVLET FEATURES
servlet.
The Java Servlets carry over the features of Java Programming Language. The key features offered by the Java
• HTTP Servlets: These are protocol dependent Servlets are as follows.
servlets, that provides support for HTTP
request and response. It is typically used to 1. Portable - Servlets feature the same Portable nature as the Java Programming Language. The Servlet program
create web apps. And has two of the most used designed in one Operating System's Platform can be run in a different Operating System Platform with ease.
methods – doGET() and doPOST() each
serving their own purpose. 2. Efficient - The Java Servlet, by its nature, provides an instantaneous response to the client's request. Also, it can
be portable and perform in every environmental condition regardless of the operating system platform.
There are three potential ways in which we can
employ to create a servlet: 3. Scalable- Java Servlets are highly scalable. Servlets use completely lightweight threads for the processes and can
simultaneously handle multiple client requests by generating various threads.
1. Implementing Servlet Interface
4. Robust-The Java Servlets are best known for their robust operating procedures. The servlets extend the features
2. Extending Generic Servlet of Java, which include.
The classloader is responsible to load the servlet class. The servlet class is loaded when the first request
The web container maintains the life cycle of a servlet
for the servlet is received by the web container.
instance.
Let's see the life cycle of the servlet: 2) Servlet instance is created
1.Servlet class is loaded.
The web container creates the instance of a servlet after loading the servlet class. The servlet instance is
2.Servlet instance is created.
created only once in the servlet life cycle.
3.init method is invoked.
4.service method is invoked. 3) init method is invoked
5.destroy method is invoked.
The web container calls the init method only once after creating the servlet instance. The init method is
used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of
the init method is given below:
The web container calls the service method each time when request for the servlet is received. If servlet
is not initialized, it follows the first three steps as described above then calls the service method. If
servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of
the service method of the Servlet interface is given below:
To write a Servlet, the user needs first to implement the Servlet Interface, directly
or indirectly, using the following import command.
import javax.servlet.*;
Once the Servlet interface is imported, and we inherit the HTTP Class, we begin
with the Java Servlet's life cycle.
In the life cycle of a servlet, we have mainly three stages, which are mentioned
below.
•init()
•service()
•destroy()
We call these methods at their respective stages. The methods are resolved by
generating the essential threads for the process to get executed.
1. init() 3. destroy()
The init() is the germinating stage of any Java Servlet. When a URL Like the init() method, the destroy() method is also called only once in the Java Servlet's
specific to a particular servlet is triggered, the init() method is invoked. entire life cycle.
Another scenario when the init() method gets invoked is when the servers
are fired up. With every server starting up, the corresponding servlets When the destroy() method is called, the Servlet performs the cleanup activities like,
also get started, and so does the init() method.
One important specialty of the init() method is the init() method only gets Halting the current or background threads
invoked once in the entire life cycle of the Servlet, and the init() method Making a recovery list of any related data like cookies to Disk.
will not respond to any of the user's commands. After that, the Servlet is badged, ready for the Garbage collector to have it cleared.
The init() method Syntax:
public void init() throws ServletException { The destroy() method Syntax:
//init() method initializing
} public void destroy() {
2. service()
The service() method is the heart of the life cycle of a Java Servlet. Right //destroy() method finalizing
after the Servlet's initialization, it encounters the service requests from
the client end. }
The client may request various services like:
•GET
•PUT
•UPDATE
•DELETE
The service() method takes responsibility to check the type of request
received from the client and respond accordingly by generating a new
thread or a set of threads per the requirement and implementing the
operation through the following methods.
•doGet() for GET
•doPut() for PUT
•doUpdate() for UPDATE
•doDelete() for DELETE
The service() method Syntax:
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
}