What is Servlet in Java with Example? | Servlet Architecture | What is the Use of Servlet in Java?

Java servlet is a key component of server-side Java development. A servlet is a small, pluggable extension to a server that enhances the server’s functionality.

A servlet is a server-side Java class that can be loaded dynamically to expand the functionality of a server.

  • Servlet is commonly used with a web server.
  • It is used to create a dynamic web page.
  • It is used to developing a dynamic web application.

What is Servlet in Java 1

While servlet can be used to extend web servers, providing a powerful, efficient replacement for the CGI script. To understand the power of Servlets, we need to step back and look at some of the other approaches that can be used to create web applications.

This tutorial on Servlet includes the following topics

Servlet Architecture

The following diagram describes the Servlet Architecture.

Servlet Architecture

Server Side Extensions are nothing but the technologies used to create dynamic web pages. In order to have the facility for dynamic web pages, web pages need a web server. In order to meet this requirement, the independent web server provides proprietary solutions in the form of APIs.

APIs help us to build programs that can run with a Web Server. Java Servlet is one of the components of APIs and sets standards for creating dynamic web applications in Java. Servlet Technology is not the only technology used for creating dynamic web pages. It is similar to other web extensions such as Common Gateway Interface(CGI) scripts and Hypertext Preprocessor (PHP). Java Servlets are most acceptable and serve the limitations of CGI.

Do Refer:

Common Gateway Interface

The Common Gateway Interface, normally referred to as CGI, was one of the first practical techniques for creating dynamic content. With CGI, a web server passes a certain request to an external program. The output of this program is then sent to the client in place of a static file.

CGI is not a programming language. It is an interface that allows input from a web browser(client) and produces an output in the form of an HTML page. A CGI script can be written in a variety of languages such as c,c++, PERL, FORTRAN, Visual Basic, and even java out of these PERL was most commonly used.

Advantages of CGI

1. CGI script works with any web browser as well as with most web servers running on Windows and Unix.

2. It is language-independent. It can be written in a variety of languages (C, C++, Perl, etc), so the developers don’t have to learn a new language.

Disadvantages of CGI

1. When a server receives a request that accesses a CGI program, It must create a new process to run the CGI program and then pass it to it. Creating a process for each such request requires time and significant server resources, which limits the number of requests a server can handle concurrently.

What is Servlet in Java 2

2. Even though a CGI program can be written in almost any language, the Perl programming language has become the predominant choice. writing a CGI script in Perl gives it a semblance of platform independence, but it also requires that each request start a separate Perl interpreter, which takes even more time and requires extra resources.

3. Another problem with CGI is that a CGI program cannot interact with the webserver, because it is running in a separate process.

Advantages of Servlet | What is the Use of Servlet in Java?

1. Servlets are highly portable across operating systems and across server implementation.

2. Servlets are also well suited for enabling client/server communications.

3. Servlets are platform-independent, the web application developed with servlet can be run on any web container such as Tomcat, Glassfish, JBoss server, etc.

4. Servlets provide better performance in terms of processing time, memory utilization because Servlets use the benefits of multithreading and for each request, a new thread is created.

What is Servlet in Java 3

5. Servlets invocation is highly efficient. Once a servlet is loaded, it remains in the server’s memory as a single object instance

6. Servlets are managed by JVM so no need to worry about memory leakage, garbage collection, etc.

7. Servlets are secure because it uses java language.

8. Servlets are protocol-independent they use any protocol like(FTP, HTTPS, NNTP, etc).

Difference Between Servlet and CGI

Servlet Common Gateway Interface(CGI)
It is portable and efficient It is not portable
Sharing of Data is Possible in Servlets. Sharing of Data is not possible.
These can directly communicate with the webserver. It can’t directly communicate with the webserver.
These are less expensive compared with CGIs CGIs are more expensive compared to Servlets.
Servlets can handle the Cookies. CGIs can’t handle the Cookies.

Servlet APIs

Servlets are build using two Packages.

javax.servlet(Basic)
javax.servlet.http(Advance)

Various Classes and Interfaces existing in these packages are as follows

Component Type Package
Servlet Interface javax.servlet.*
ServletRequest Interface javax.servlet.*
ServletResponse Interface javax.servlet.*
GenericServlet Class javax.servlet.*
HttpServlet Class javax.servlet.http.*
HttpServletRequest Interface javax.servlet.http.*
HttpServletResponse Interface javax.servlet.http.*
Filter Interface javax.servlet.*
ServletConfig Interface javax.servlet.*

What is Servlet Container in Java?

Servlet Container also known as Servlet Engine is an integrated set of objects and provides run time environment for JavaServlet Components. In Other Words, it is a system that manages the Java Servlet Components on top of Web Server to handle the Web Client Requests.

Services Provided by the Servlet Container

  • Network Services
  • Decode and Encode MIME based messages
  • Manage Servlet container
  • Resource management
  • Security Service
  • Session Management