Today we all are aware of the need of creating dynamic web pages i.e the ones which have the capability to change the site contents according to the time or are able to generate the contents according to the request received by the client. If you like coding in Java, then you will be happy to know that using Java there also exists a way to generate dynamic web pages and that way is Java Servlet. But before we move forward with our topic let’s first understand the need for server-side extensions.
Servlets are the Java programs that runs on the Java-enabled web server or application server. They are used to handle the request obtained from the web server, process the request, produce the response, then send response back to the web server.
Properties of Servlets :
- Servlets work on the server-side.
- Servlets are capable of handling complex requests obtained from web server.
Execution of Servlets :
Execution of Servlets involves six basic steps:
- The clients send the request to the web server.
- The web server receives the request.
- The web server passes the request to the corresponding servlet.
- The servlet processes the request and generates the response in the form of output.
- The servlet sends the response back to the web server.
- The web server sends the response back to the client and the client browser displays it on the screen.
The following diagram shows the servlet architecture:
Need For Server-Side Extensions
The server-side extensions are nothing but the technologies that are used to create dynamic Web pages. Actually, to provide the facility of dynamic Web pages, Web pages need a container or Web server. To meet this requirement, independent Web server providers offer some proprietary solutions in the form of APIs(Application Programming Interface).
These APIs allow us to build programs that can run with a Web server. In this case , Java Servlet is also one of the component APIs of Java Platform Enterprise Edition which sets standards for creating dynamic Web applications in Java.
Before learning about something, it’s important to know the need for that something, it’s not like that this is the only technology available for creating dynamic Web pages. The Servlet technology is similar to other Web server extensions such as Common Gateway Interface(CGI) scripts and Hypertext Preprocessor (PHP). However, Java Servlets are more acceptable since they solve the limitations of CGI such as low performance and low degree scalability.
What is CGI ?
CGI is actually an external application which is written by using any of the programming languages likeC or C++ and this is responsible for processing client requests and generating dynamic content.
In CGI application, when a client makes a request to access dynamic Web pages, the Web server performs the following operations :
- It first locates the requested web page i.e the required CGI application using URL.
- It then creates a new process to service the client’s request.
- Invokes the CGI application within the process and passes the request information to the server.
- Collects the response from CGI application.
- Destroys the process, prepares the HTTP response and sends it to the client.
So, in CGI server has to create and destroy the process for every request. It’s easy to understand that this approach is applicable for handling few clients but as the number of clients increases, the workload on the server increases and so the time taken to process requests increases.
Difference between Servlet and CGI
|Servlet||CGI(Common Gateway Interface)|
|Servlets are portable and efficient.||CGI is not portable|
|In Servlets, sharing of data is possible.||In CGI, sharing of data is not possible.|
|Servlets can directly communicate with the web server.||CGI cannot directly communicate with the web server.|
|Servlets are less expensive than CGI.||CGI are more expensive than Servlets.|
|Servlets can handle the cookies.||CGI cannot handle the cookies.|
Servlets are build from two packages:
Various classes and interfaces present in these packages are:
Advantages of a Java Servlet
- Servlet is faster than CGI as it doesn’t involve the creation of a new process for every new request received.
- Servlets as written in Java are platform independent.
- Removes the overhead of creating a new process for each request as Servlet doesn’t run in a separate process. There is only a single instance which handles all requests concurrently. This also saves the memory and allows a Servlet to easily manage client state.
- It is a server-side component, so Servlet inherits the security provided by the Web server.
- The API designed for Java Servlet automatically acquires the advantages of Java platform such as platform independent and portability. In addition, it obviously can use the wide range of APIs created on Java platform such as JDBC to access the database.
The Servlet Container
Servlet container, also known as Servlet engine is an integrated set of objects that provide run time environment for Java Servlet components.
In simple words, it is a system that manages Java Servlet components on top of the Web server to handle the Web client requests.
Services provided by the Servlet container :
- Network Services : Loads a Servlet class. The loading may be from a local file system, a remote file system or other network services. The Servlet container provides the network services over which the request and response are sent.
- Decode and Encode MIME based messages : Provides the service of decoding and encoding MIME-based messages.
- Manage Servlet container : Manages the lifecycle of a Servlet.
- Resource management : Manages the static and dynamic resources, such as HTML files, Servlets and JSP pages.
- Security Service : Handles authorization and authentication of resource access.
- Session Management : Maintains a session by appending a session ID to the URL path.
Co-Author : Kartik Thakral
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Attributes in Servlets | Java
- Java Servlets | Need of Filters
- Simple Bill Splitter Application using Java Servlets
- Difference between Applets and Servlets
- Java | MIDI Introduction
- Introduction to Processing | Java
- Introduction to Java NIO with Examples
- Introduction to Java
- Singleton Design Pattern | Introduction
- Introduction to Hibernate Framework
- Introduction to JSP
- JavaTuples | Introduction
- Apache JMeter - An Introduction
- Microservices Introduction
- Introduction to Spring Framework
- Introduction to Spring Boot
- Introduction and Working of Struts Web Framework
- Introduction to JDBC
- Introduction to the Spring Data Framework
- Introduction of BlueJ
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.