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:
- Servlets operate on the server side.
- 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:
- The client sends the request to a web server.
- The request was obtained by the web server.
- The web server passes the request to the relevant servlet.
- The request is processed by the server and the output is generated in the form of output.
- The servlet then sends the response back to the web server.
- 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:
- The first step is to locate the web page requested by the client.
- Then, a new process is created to fulfill the client’s request.
- Invokes the CGI application within the process and passes the requested information to the application.
- Obtain the response from the CGI application.
- Terminates the process, then creates the HTTP response, and then finally sends it to the client.
Difference between CGI and Servlet
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.