It is a Java Programming language class. Servlet program in Java is run on the Java-enabled web application or server and used to handle requests received from the web server, process and produce the response to the request, and then send the response back to the web server.
These servlets work on the server-side. It is used to extend server capabilities to host applications to host request-response models in the programming. It can handle any type of request and is capable of handling complex requests efficiently.
Servlet programs employed using Java do not rely on the platform. It creates processes depending on the type of client request it receives. It runs on JVM (Java Virtual Machine). Java Servlet program complies with Java Bytecode. Java Servlet is able to resist attacks. Other advantages include faster speed, portability, and efficiency.
It translates and compiles scripts before processing them. Java Servlet enables data sharing. It can directly communicate with the webserver and handle the cookies. It is less expensive than other methods such as CGI (Common Gateway Interface)
Java Servlet Architecture
The Java Servlet Architecture includes a communication interface, client and server requirements, used protocol, and involved programming language and software.
Java servlet reads received data from the client browser that can be in any form such as HTML (Hypertext Markup Language), custom HTTP (Hypertext Transfer Protocol) program, an applet or implicit HTTP request data. Then it processes received data by communicating to an existing database and carries out other steps before generating the results.
This result (data or document) is sent to the clients in various formats like text (HTML or XML (Extensible Markup Language)), image (binary). In the end, it sends an implicit HTTP response to the client to inform them of the type of document being returned.
Stages of Java Servlet Life Cycle
Java Servlet has a life cycle of four stages that includes loading Servlet, initialising Servlet, handling a request, and destroying Servlet. The Servlet container deploys and loads all received servlets as the web server starts up.
It initialises the servlet by calling the method of int(). The Servlet instance successful instantiation and its service indication are indicated by calling the Servlet.init() method. The client’s request is processed by the Servlet using service() method. The client request is informed to the Servlet by using this method.
Lastly, this created Servlet is terminated by calling destroy() as a signal of the end of the Servlet instance. int() and destroy() methods are called once in the lifetime of a Java Servlet. Finally, the JVM garbage collector collects the servlet garbage and concludes the life cycle of a servlet.
Also Read: Java Architecture & Components Explained
Execution Steps of Java Servlets
Servlets are executed using six fundamental steps, as given below:
- The client sends the request to the web application/server.
- The web application/server receives the request.
- The web application/server sends the request to the appropriate servlet.
- The servlet processes the request through its life cycle and generates a response as an output.
- The servlet sends the response back to the web application/server.
- Lastly, the web application/server sends this request back to the client, and the client’s browser displays it on the web page.
Java Servlet Classes and Interfaces
Though Java Servlet can create dynamic web pages, this facility is provided to web pages by a container or web server. Independent web server providers offer proprietary solutions like APIs (Application Programming Interface) to meet this requirement.
Using these APIs, one can build and run programs with a web server. Java Servlet is the Java Platform Enterprise Edition, one of the component APIs that sets standards in dynamic web applications created in Java.
Java Servlet API has two important packages to encapsulate all the important interfaces and classes, as given below:
- javax.servlet (Basic)
- Javax.servlet.http (Advance)
The table below contains a few important classes and interfaces of Java Servlet.
|Servlet||Type||Package||Declaration of LifeCycle methods of the servlet|
|ServletConfig||Interface||javax.servlet||It allows the servlet for initialisation methods|
|ServletContext||Class||javax.servlet||It enables the servlet for logging the access and the access information|
|ServletRequest||Interface||javax.servlet||It is used to read the data from the client request|
|ServletResponse||Interface||javax.servlet||It is used to write the data to client’s response|
|GenericServlet||Class||javax.servlet||It implements the Servlet and Servlet.config Interface|
|ServletInputStream||Class||javax.servlet||It provides the input stream to read requests received from the client|
|ServletOutputStream||Class||javax.servlet||It provides an output stream to write responses to the client|
|ServletException||Class||javax.servlet||The indication of the unavailability of the servlet|
|HttpServlet||Interface||javax.servlet.http||The methods for handling HTTP Request & Response|
The Servlet Container
It is an integrated set of objects that provides a run-time environment to Java Servlet components. It provides services like network, decoding and encoding MIME (Multipurpose Internet Mail Extensions)- based messages, Servlet container management, resource and session management, and security services.
Must Read: Interesting Java Projects & Topics
Thus, Java Servlets are used to achieve dynamic web pages along with increased speed and flexibility while processing client requests on the web server.
If you’re interested to learn more about Java, OOPs & full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.