JAVA Servlet API


To understand the JAVA Servlet Application Programming Interface, we should look at what a servlet is fundamentally. A servlet is mainly a procedure or technique to create web-based applications. The term servlet is used as the control lies at the server it creates dynamic wen pages for clients to handle and modify. With the help of the Servlet API and its classes and interfaces, users will be able to code server programs for web applications.

Block Diagram of the JAVA Servlet API

The following diagram will extend the functionalities of a servlet graphically. A servlet can be thought of as an extended part of a server that aids it in tasks such as responding to requests. After a client has lodged a request, if the server is busy at the time, the incoming request will be directed to a servlet. The servlet will then connect with the server, produce an appropriate response, and relay it back to the client.

JAVA Servlet API

Packages in the JAVA Servlet API

There are primarily two packages in the Servlet API. They are as follows along with their JAVA documentation:

  • servlet – “This package contains several classes and interfaces that describe and define the contracts between a servlet class and the run-time environment provided for an instance of such a class by a conforming servlet container.”
  • servlet.http – “The javax.servlet.http package contains several classes and interfaces that describe and define the contracts between a servlet class running under the HTTP protocol and the runtime environment provided for an instance of such a class by a conforming servlet container.”

Both these packages have been imported into the sample code section of this article. These packages prove to be the building blocks if you want to code a web-based application in JAVA.

Advantages of using Servlets

With the previously used CGI technology for scripting a server-side program, responses would get detailed if many clients sent requests at the same time. A process was initiated for each request in the CGI system and the number of initiations was also limited. It was also difficult to use CGI as it was mainly based in C and C++. However, the Servlet technology has brought with it several fixes for better user experience. Unlike CGI, instead of a process being started, a thread is created for each request. This makes the process much faster and does not clog the servers with requests. The JAVA virtual machine is responsible for managing the Servlet technology. Therefore, developers need not worry about space-related issues or garbage collection.

Example and Code

/* The following example is a simple rendition of the words ‘Sample Servlet Program Successful’ that should appear on your web browser when the compiled program is run. We will be using the basic concepts of HTML and extending its syntax into the code. */

//Importing all the class libraries of the servlet API
import javax.servlet.*;
import javax.servelt.http.*;

public class Sample extends HttpServlet                                //The Http Servlet class is part of the Servlet API
private String text;

public void init() throws Servlet Exception {
text = “Sample Servlet Program Successful”;

public void doGet (HttpServletRequest req, HttpServletResponse rep)
throws ServletException, IOException {
response.setContentType(“text/html”);                 // this determines the content type
PrintWriter out = response.getWriter();
out.println(“<h1>” + message + “</h1>”);      //This syntax in html determines header type of the text

public void destroy() {
//As there is nothing to delete, we will keep this method empty


To be able to generate an output, there are a series of steps that you need to follow. After saving this ‘.java’ file, make sure to add the file path to the CLASSPATH. Once done, navigate to the ServletDevel directory and run the program using the following code
“$ javac”

Now open up the .xml file and you will see the following output on your screen:
“Sample Servlet Program Successful”


  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.