Core Java Concepts and Syntax

Understanding Servlets in Java

Understanding Servlets in Java

Introduction

A web page is broadly classified into two categories: Static webpage and Dynamic web page. In a static page, when a web server gets a request for a web page then it immediately sends the request to the client without doing any additional process.

In today’s modern world, we need to create dynamic pages. A dynamic web page is capable of altering the web page's contents as per the time.

Java Servlets

In java, we have java servlets using which we can create dynamic web pages easily. Before moving further let's try to understand why we need server-side extensions.

Servlets are java programs that can be executed on the JAVA-supported web server. Their main responsibility includes handling requests received from the web server, processing the request, generating the response, and then sending back a response to the server.

Java servlets have the following properties:

  1. Servlets operate on the server side.
  2. They are capable of handling even the complex requests obtained from the server.

A servlet has the following architecture:

The servlets can be executed in three basic steps:

  1. The client sends the request to a web server.
  2. The request was obtained by the web server.
  3. The web server passes the request to the relevant servlet.
  4. The request is processed by the server and the output is generated in the form of output.
  5. The servlet then sends the response back to the web server.
  6. The client then receives a response from the web server and it gets displayed on the client’s browser.

What is CGI?

CGI is regarded as an external application whose source code is written using the programming languages like C or C++. This application is responsible for processing requests that are requested by clients.

In the CGI application, when a request is made by the client to get the dynamic Web pages, the Web server carries out the following operations:  

  1. The first step is to locate the web page requested by the client.
  2. Then, a new process is created to fulfill the client’s request.
  3. Invokes the CGI application within the process and passes the requested information to the application.
  4. Obtain the response from the CGI application.
  5. Terminates the process, then creates the HTTP response, and then finally sends it to the client.

Difference between CGI and Servlet

CGI

Servlet

Data sharing is not allowed

Data sharing is allowed

It is not portable

It is portable

It can’t handle cookies

It can handle cookies

More expensive than servlets

Less expensive than CGI

Doesn’t have the functionality to communicate to web server

They can directly communicate to a web server

Servlets API

Servlets are made up of two packages: javax.servlet and javax.servlet.http

Advantages of a Java Servlet

Some of the advantages of java servlets are given below:

1. In Servlet, new processes are not created. Therefore, it is faster than CGI.

2. Servlets generally are platform-independent.

3. These are the server-side component. Therefore, the servlet inherits the security provided by the Web server.

Conclusion

In this article, we discussed java servlets in detail. We compared CGI and java servlets in java and finally, we discussed the advantages of java servlets.