Introduction with Servlet in Java

Servlet is a server-side programming language used to create dynamic web applications. Servlet is a java program that runs on the server and is used to create a web application.

In this servlet tutorial, I am going to cover the Introduction to Servlet and some information about the servlet that you should know before start writing servlet code.

What is Servlet?

Servlet is a Java program that runs on the webserver. It receives HTTP requests from clients and responds with an HTTP response.

  • It is a middle layer between a request coming from Browser/Client and database or any application at Server. Servlet handles the client’s request and returns a customized or dynamic response for each request.
  • The main task of Servlet is managing, loading, creating requests, and response objects.

Servlet Architecture

Servlet is a class which implements javax.servlet.Servlet.It doesn’t implement it directly it extend the class javax.servlet.GenericServlet or javax.servlet.http.HttpServlet.

Client: The web browser act as a client. The client sends the request to the Web server.
Web Server: Web Server processes it and gives back the response in the form of an HTTP response.
Web Container: Web container interacts with Java Servlet. It does the mapping of the URL.

What is Web Application?

Web applications are applications that reside on the Server and used to build dynamic web pages. Dynamic web pages are the pages on a server that generate dynamic data. (Like we see weather information changing every time we refresh the page).

The above diagram shows the Architectural view of a Web application using Servlet. A client requests the data to the Server. The Server analyzes it, processes it, and gives back the response to the Web Browser.

  • First, the Client Requests the data through a web browser to the Web Server.
  • If it is a static page (predefined HTML page) it will process it and give it back to the client in the form of HTTP Response.
  • If it is a Dynamic Page, it will invoke the servlet program then it will see in the web Container and it will give the data to the webserver and in turn, the Web Server will provide data to the Client.
  • This is how Servlet Web application works.

As Servlet uses Java Technology, web applications build using Servlet are Robust, Secured, and Scalable.

The package used for Servlet are:


History of the servlet

The Internet was designed to transfer files from one place to another for that purpose we have an FTP protocol. But we wanted to look at the data So, then HTML came into existence.FTP was able to transmit the HTML documents. But later on, we started using an HTTP protocol for HTML pages as it is more efficient than FTP.

Then, We were able to access HTML pages over the net. But the thing here is they are predefined data But what if we want to access some live data that keeps on changing? that was a problem here. So the developer came with CGI which is the Common Gateway Interface.

CGI could reach beyond the server and can access data from the database. At that, that CGI was a breakthrough for the web application.

It was able to deliver dynamic content over the internet. But CGI has its limitation some glitch was found by the developer that the CGI was using processes which in turn leading to load on System.

As we know, in 1995 Sun Microsoft system released Java as an Open Source which led to Server-Side Programming. Then in 1997, Sun announced an interface Called ‘Servlet’.Unlike CGI, Servlet used a single process that leads to less load as compared to CGI and Became more efficient, robust, secured.

Advantages of Servlet:

  •  It has good performance because it creates threads instead of processes inside the system.
  • Robust because of the Java Language.
  •  Scalable because of Java Language.
  •  Secured because of Java Language.
  • ·Load is less as compared to CGI.

What was before Servlet?

CGI (Common Gateway Interface)

In GGI, it enables the webserver to call an external program and pass HTTP request information to the external program to process the request.

Why don’t we use CGI?

  • For each request, it starts a new process.
  • It uses Platform independent language.
  • It takes more time to send the response if the client increases.
  • The load on the processor is more here.

In the next article, we will discuss How to Setup the Environment for Servlet application to run i.e Environment Set Up for Servlet.