In this era of the digital world, people search for information or data over the Internet is a task being carried out daily. There has been a constant increase in the number of people using the internet to search for all the information they need.
Do you know from where people get all this information? The answer to this question is dynamic websites. Yes, all such information that people need over the internet, which is stored in databases, is accessed through these dynamic websites and displayed over their webpages.
When we call a website as dynamic then it simply means that the web pages in that particular website contains the information that changes, depending on the viewer or depending on the request made by the client (user’s browser). However, a static website is one in which the information remains the same or static for every viewer.
Depending upon whether you want the content in the website to respond to the user’s request or not, these dynamic and static websites can be designed.
In this blog, we will discuss in-depth about Java Servlets technology and additional concepts associated with it.
To understand the concept of Java Servlets, it is necessary for the readers to have a basic knowledge of Java programming language. I would recommend our readers to first read Java | How to Learn Java from Scratch in 2019 blog in order to have a better understanding of the fundamental concepts of the Java programming language.
The overall aim of the blog is to help our readers understand:
Basic web terminologies
What are Java Servlets?
Purpose of using Java servlets
How does a Servlet work?
Different types of Java Servlet
Servlet classes and interfaces
Advantages of Java Servlets.
Basic web terminologies:
Before we start understanding what Java Servlets are, let us see few basic web terminologies that are used throughout this discussion.
- Client: A client is a computer device or a software application, such as web browser, that sends requests and makes use of the resources provided by another computer or server. This application runs on a user’s local computer or workstation. Client is the one who initiates communication with Server.
- Server: A server is a computer device or a software application that accepts and responds to the requests made by another computer known as client. Components of Server provide service to one or many Clients. Based on the service the Servers provide, they are classified as Web Server and File Server.
- HTTP: HTTP stands for HyperText Transfer Protocol. It is a protocol used to establish the connection between client and server.
- HTTP Request: HTTP Request is a packet of information sent by a client to the server.
- HTTP Response: HTTP Response is a packet of information sent by the server to the client in response to a request made earlier by the client.
- Web Server: Web server is a program that uses Hypertext Transfer Protocol (HTTP) to serve the files from web pages to users in response to their request that are forwarded by their computer’s HTTP client.
- Web container: Web container is a component of a web server used to interact with Java Servlets. It is also called a Servlet container.
- Web application: Web application or web app is an application program which is stored on a remote server and delivered over the internet through a browser interface.
Now let us see what are Java Servlets?
Java servlets can be defined in many ways:
- In simple terms, Servlet is a server-side driven technology used to create dynamic websites. The Servlet receives a request from the clients and generates a response based on that request.
- Java Servlets are programs which run on the web server and act as a middle layer between a request coming from the web browser (client) and database on the HTTP server.
Purpose of using Java Servlets
Now that we have discussed Java Servlets, let us move on to understanding the necessity of specifically using Java Servlets to build a dynamic website. To know this, we need to understand CGI i.e. Common Gateway Interface, a technology used prior to Java Servlets.
Now, let us understand what is CGI?
CGI is a technology or standard method used to generate dynamic content on web pages and web applications. Common Gateway Interface, when implemented on a web server, provides an interface between a web server and programs that generate web content.
But CGI technology has some limitations such as:
- Every time a new process is created: A new CGI process has to be created for every client request.
- Takes more time to respond: As the number of clients increases, it takes more time to respond.
- Platform dependent: CGI technology is platform dependent.
To overcome these limitations of CGI, Java Servlets were introduced by Java programming language. Some of the advantages of Java Servlets are:
- Separate threads are created: Web container creates threads to handle multiple client requests.
- Platform-independent: As Servlets are written using Java, they are platform-independent.
These are some points which help us understand the purpose of using Java Servlets.
Now, let us have a look at the architecture of Servlet. Under Java Servlet architecture we will discuss a few important points such as:
- Communication interface
- Type of protocol used
- Requirements about the client and the server, etc.
In web communication, both the client and the server communicate over the internet(web) as a medium. When these are connected together, the client system can be called as “web client” and server can be called as “web server”.
HyperText Transfer Protocol (HTTP) is the standard protocol used in the web, hence, the client is known as HTTP client and server is known as HTTP server.
The above diagram shows the architecture of Servlets and Java Servlets perform the following major tasks
The client sends data through a web browser, which may be a HTML form on a web page, an applet, a custom HTTP client program, or cookies, etc.
This request is received by the web server and it passes the request to the relevant servlet.
Servlet processes the data sent by the client (browser) and generates the results. This process may include communicating to a database, invoking a web service, etc. These results are sent back to the web server.
Web server sends the results back to the client (browser). These results can be in a variety of formats such as HTML, XML or Excel formats, etc.
This constitutes the overall steps involved in the execution of Java Servlets.
The Servlet life cycle includes the following stages:
- Loading and Instantiation of a Servlet
- Initialization of a Servlet object
- Request handling by a Servlet
- Destroying a Servlet.
- Loading and Instantiation of a Servlet
When the user request is received by the Web Server/Servlet Container it starts loading the Servlet class using Classloader. Once the Servlet class is loaded, the Servlet container instantiate the object of this Servlet class using no-argument constructor.
The number of instances created by a Servlet container depends on the following criteria:
- The Servlet container creates multiple instances of this Servlet class if it has implemented SingleThreadModel interface, if required, each instance will handle an individual user request
- The Servlet container will create one instance of this Servlet class if it has not implemented SinlgeThreadModel interface. This instance will be used to handle all the user requests.
- Initialization of a Servlet object
After successful loading of the Servlet class, the Servlet container calls the init (ServletConfig) method of Servlet class to initialize the Servlet object.
The init (ServletConfig) method is called just once in the Servlet life cycle. It passes the references of ServletConfig object, which provides access to the Servlet’s configuration data or its initialized parameters. The Servlet container changes the state of the Servlet to active state only after the successful initialization of the object. This makes the Servlet ready to handle the requests.
- Request Handling by a Servlet
Once the Servlet object is initialized by the Servlet container, the Servlet container performs either of the following functions:
- The ServletRequest and ServletResponse objects are created by Servlet container to handle non-http request which are made by the client and send back an appropriate response or
- The HttpServletRequest and HttpServletResponse objects are created by Servlet container in order to handle HTTP requests made by the client and send back the appropriate HTTP response to the client.
- Destroying a Servlet
Once the Servlet class has been taken out of the service container the Servlet calls the destroy() method.
How Servlets works?
Once you understand the Servlet life cycle then it is easier to know how Servlets work.
Initially, loading of the Servlet is important. Here, whenever a web server starts up, the Servlet container loads all the Servlets. Once all the Servlet classes are loaded, then the Servlet container creates an instance of a Servlet. These instances of a class are created only once.
Now, the init() method is invoked which initialises the Servlet class. Each time a client sends the HTTP request to a web server, the servlet container calls the service() method to pass request and response objects.
When the web server stops, the web container unloads all the Servlets and calls the destroy() method for each initialized servlets.
Different types of Java Servlets
There are 2 different types of Java Servlets available. These are:
- Generic Servlet: Generic Servlet is a protocol-independent servlet. It can handle any type of requests. It’s a base class servlet using which all other Servlets are derived. Some protocols supported by Generic Servlets are:
Generic Servlet implements Servlet interface, Servlet config interface, and Serializable interface. It provides implementation of Servlet interface methods and Servlet config interface method but will support the implementation of the Service method.
- HTTP Servlet: HTTP Servlet extends Generic Servlet class. It is a HTTPdependent Servlet. It is a set of rules which allows the web browser and servers to communicate. HTTP Servlet override the doGet() method or doPost() method or both.
Servlet classes and interfaces
Servlets API is used to create servlets. The javax.servlet and javax.servlet.http are the 2 packages that represent classes and interfaces for Servlet API.
javax.servlet: javax.servlet is a package which contains classes and interfaces that are used for a Servlet.
javax.servlet.http: javax.servlet.http is a package which contains classes and interfaces that are responsible for http request only.
Some of the classes and interface present in javax.servlet package are as follows:
Some of the classes and interfaces present in javax.servlet.http package are as follows:
Advantages of Java Servlets
Now that we have understood what Java Servlets are, and the various concepts associated with it, let us see some of the advantages of Java Servlets.
- etter performance: Servlets have better performance
- Platform-independent: Servlets are written in Java and hence they are platform-independent
- Secured: Servlets are more secured
- Provides complete functionality: A complete functionality of the Java class libraries is available to a Servlet
- Robust: Since Java Virtual Machine manages Servlets, there is no need to worry about memory leak, garbage collection, etc.
Conclusion:The focus of this entire discussion is to introduce our readers to Java Servlets. For simplicity, this blog discusses about the basic concepts associated with Java Servlets, our readers can find a detailed discussion about these concepts in our online Java courses well taught by the industry experts.
Let us know which topic of the above discussion you found more valuable and why so? We will be happy to hear your thoughts.