Introduction with Servlet in Java

As we see dynamic web sites on the internet today if one wants to code that site in Java language so it is possible due to Servlet in Java language. Servlet is a java program that runs on the server and is used to create a Web Application. Java Language is used in creating Servlet. Servlets can handle complex type requests.

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.

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.
The main task by Servlet is managing, loading, creating requests, and response objects.

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 call 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 made using Servlet are Robust, Secured, and Scalable.

The package used for Servlet are:

·javax.servlet
·javax.servlet.http

History of the servlet

The Internet was designed to transfer files from one place to another for that 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 now we use the HTTP protocol for HTML pages as it is efficient than FTP.

Then, We were able to access HTML pages over the net. But the thing here is they are predefined data But if we want to access some live data that is changing so 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 thread instead of process 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.