Introduction
This article provides a brief introduction to servlets in Java. The Netbeans IDE is used for the example. We describe the following terms related to servlets, why we need servlets, what CGI is, how to establish communication between a server and a client, the differences between doGet() and doPost() methods, etcetera.
What is servlet
Servlet is a class to extend the properties of a server, It is used to design web-based applications. It is an Application Programming Interface (API) containing predefined interfaces and helper classes. Implementation of some of the interface is provided by application developers and the implementation of the rest of the interface is provided by the server vendor.
Why we need servlets
Before servlets there was another API called Common Gateway Interface (CGI) for developing web-based applications. Let's have a look at CGI.
What Common Gateway Interface (CGI) is
It is a protocol that was developed to facilitate communication between web server and a request processing program. In CGI based web applications the request process program was developed in C/C++ or Perl. The programs are called CGI scripts.

1.0: The client sends a request for some dynamic content to the server.
1.1: On receiving the request a process is submitted by the server to create a GUI and a Script.
1.2: The request is process by a CGI script and dynamic generated contents are provided to the server.
1.3: Dynamically generated content is sent as a response by the server.
CGI based applications have the following drawbacks:
- Execution of CGI scripts are process-based. Process-based execution creates more overhead and limits the scalability.
- CGI scripts were platform-dependent.
In that context and due to those drawbacks, a new technology is required; in other words servlets.
Servlets
Technology that eliminates both of those drawbacks as in the following:
- The first drawback is removed by providing a thread-based processing model.
- The second drawback is removed by facilitating the development by request processing programs in Java.
Now understand servlets
javax.servlet.*; and its sub-packages contains classes and interfaces of the servlet API.
At the core of the servlet API is an interface name "Servlet" as in the javax.servlet.Servlet package. This interface provides servlet life cycle methods. Implementation of its needs are provided by all servlets.
It has the following method,
- public void init(setConfig config);
This method is invoked only once just after a servlet object is created.
It is used by the server to provide a refrence of the servlet configuration object to the servlet. The Servlet config is an interface of the servlet API. Implementation of it is provided by the servlet vendor.
- public void service(SentRequest req, SentResponse res)throws ServletException, IOException;
This method is invoked by the server each time a request is received for the servlet.
This method is used by the servlet for processing a request. In this method a reference of an object is type ServletRequest and ServletResponses are provided by the server. This objects facilitates transfer of input between servlet and server.
- public void destroy();
This method is invoked only once just before a servlet is unloaded.
- public ServletConfig getServletConfig();
Used to obtain the reference of the ServletConfig object from the servlet.
- public String getServletInfo();
Used to obtain the description of the servlet.
Servlet Implementation
In order to define a servlet implementation of a Servlet, an interface must be provided.
To facilitate indirect implementation of the Servlet interface, Sun Microsystems provides a helper class named GenericServlet. It is an abstract class. It implements servlet interface and provides implementation of all its methods except service().
Syntax
- //implementation of Servlet through GenericServlet
- public class MyServlet extends Generic Servlet
- {
- //service() method is defined
- }
How we create a connection between client and server
Communication between the client and server is done by the HTTP protocol. This protocol supports the following requests:
- Get
- Post
- Head
- Put
- Delete
- Trace
- Option
- Connect
The Servlet interface provides a protocol-independent request processing model javax.servlet. The HTTP package provides classes and interfaces for the HTTP based request processing.
Some commonly used HTTP classes are,
- HttpServletRequest
- HttpServletResponse
- HttpServlet
Commonly used methods of HttpServlet are doGet() and doPost(). These are used to process HTTP get and post requests respectively.
Syntax
- public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException;
- public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException;
Syntax to implemet HttpSpecific servlet
- public MyServlet extends HttpServlet
- {
- //Either doGet or doPost()
- // or both are used
- }
In the syntax above we see that there are two methods, doGet() and doPost(), Now understand the differences between them.
There are the following differences between the doGet() and doPost() methods.
- Conventionally get requests are used for static contents from the server and post requests are used for getting dynamic content from the server.
- The technical difference is that a getRequest request is sent as part of a header size of a HTTP package header is fixed hence only a limited amount of data can be sent as part of a getRequest.
When PostRequest data is sent as part of a body, the size of the HTTP package body can be unlimited, hence an unlimited amount of data can be sent with a postRequest.
- When getRequest requested data is append to the URL, it is visible in the address bar. The postRequest request data is not append to the URL, hence it is not visible in the address bar of the browser.
- When getRequest requested data is transmitted over the network it is submitted by the user whereas postRequest requested data is encrypted using a standard 32-bit encryption algorithm and is transmitted over the network.
- getRequest is idempotent postRequest is not.














Sam HobbsPosted Sep 14, 2013, 4:49 PM
Is Perl "platform-dependent"? I thought it is not and if not then platform dependence is not a disadvantage of CGI using Perl. Correct?
Sam HobbsPosted Sep 14, 2013, 4:46 PM
When describing CGI, it is important to explain its realtionship with HTML forms. When CGI scripts are used, the first thing that normally happens is a HTML page is sent to the browser with one or more forms. Each form (usually only one) has a submit button (normally) that can execute a CGI script but the submit button can do other things. Until a submit button is pressed that executes a CGI script, there is nothing special about the HTML with the form; there is no CGI script executing until after the submit button is pressed, correct? Is it not true that CGI can be processed in the browser by Java if the submit button action specifies that another HTML page is to be sent to the browser when the form is submitted? The best explanation of the need for Servlets is to note that Java originally executed in the browser.