Site icon SimplivLearning Blog

Java Servlets | Introduction to Servlets and its Architecture

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.

Now how do you create such dynamic websites? There are several technologies such as Servlets introduced by Java, ASP, Node.js that help us to create dynamic website.

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:

Before we start understanding what Java Servlets are, let us see few basic web terminologies that are used throughout this discussion.

Now let us see what are Java Servlets?

Java servlets can be defined in many ways:

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:

To overcome these limitations of CGI, Java Servlets were introduced by Java programming language. Some of the advantages of Java Servlets are:

These are some points which help us understand the purpose of using Java Servlets.

Servlet architecture

Now, let us have a look at the architecture of Servlet. Under Java Servlet architecture we will discuss a few important points such as:

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.

Servlet lifecycle:

The Servlet life cycle includes the following stages:

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:

  1. 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
  2. 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.

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.

Once the Servlet object is initialized by the Servlet container, the Servlet container performs either of the following functions:

  1. 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

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:

  1. 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:
  2. HTTP
  3. FTP
  4. SMTP

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.

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:

Javax.servlet
Classes Interfaces
GenericServlet Servlet
ServletContextAttributeEvent ServletConfig
ServletContextEvent ServletContext
ServletInputStream ServletContextAttributeListner
ServletOutputStream ServletContextListner

Some of the classes and interfaces present in javax.servlet.http package are as follows:

Javax.servlet.http
Classes Interfaces
HttpServlet HttpServletRequest
HttpServletRequestWrapper HttpServletResponse
HttpServletResponseWrapper HttpSession
HttpSessionBindingEvent HttpSessionActivationListener
HttpSessionEvent HttpSessionAttributeListener

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.

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.

Exit mobile version