MediumJava Servlets. Servlet technology is used to create a… | by Katuri Santhosh Kumar | Medium
Sitemap

Java Servlets

Servlet technology is used to create a web application .

--

What is a Java Servlet?

Press enter or click to view image in full size

We define a Java Servlets are one of the most popular and widely used technologies for building dynamic web applications. They are server-side components that allow web developers to design and deploy dynamic web pages using the Java Programming Language. It implements a typical servlet in the client-server architecture, and the Servlet lives on the server-side.

Press enter or click to view image in full size

There are several varieties of interfaces and classes available in the Servlet API. Some of them are as follows:

  • HTTP Servlet
  • Generic Servlet
  • Servlet Request
  • Servlet Response

With the definition, interfaces, and classes of Java Servlets discussed, we will now advance into our next topic, learning about the Java Servlet’s Architecture.

Java Servlet Architecture

Press enter or click to view image in full size

We can also consider the software architecture of a servlet as the life cycle of the Java Servlet. To write a Servlet, the user needs first to implement the Servlet Interface, directly or indirectly, using the following import command.

import javax.servlet.*;

Once the Servlet interface is imported, and we inherit the HTTP Class, we begin with the Java Servlet’s life cycle.

In the life cycle of a servlet, we have mainly three stages, which are mentioned below.

  • init()
  • service()
  • destroy()

We call these methods at their respective stages. The methods are resolved by generating the essential threads for the process to get executed.

Next, we will learn the Life-Cycle of a Java Servlet in a detailed manner.

Java Servlet Life-Cycle

The Java Servlet Life cycle includes three stages right from its start to the end until the Garbage Collector clears it. These three stages are described below.

  1. init()
  2. service()
  3. destroy()

1. init()

The init() is the germinating stage of any Java Servlet. When a URL specific to a particular servlet is triggered, the init() method is invoked.

Another scenario when the init() method gets invoked is when the servers are fired up. With every server starting up, the corresponding servlets also get started, and so does the init() method.

One important specialty of the init() method is the init() method only gets invoked once in the entire life cycle of the Servlet, and the init() method will not respond to any of the user’s commands.

The init() method Syntax:

public void init() throws ServletException {

//init() method initializing

}

2. service()

The service() method is the heart of the life cycle of a Java Servlet. Right after the Servlet’s initialization, it encounters the service requests from the client end.

Get Katuri Santhosh Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

The client may request various services like:

  • GET
  • PUT
  • UPDATE
  • DELETE

The service() method takes responsibility to check the type of request received from the client and respond accordingly by generating a new thread or a set of threads per the requirement and implementing the operation through the following methods.

  • doGet() for GET
  • doPut() for PUT
  • doUpdate() for UPDATE
  • doDelete() for DELETE

The service() method Syntax:

public void service(ServletRequest request, ServletResponse response)

throws ServletException, IOException {

}

3. destroy()

Like the init() method, the destroy() method is also called only once in the Java Servlet’s entire life cycle.

When the destroy() method is called, the Servlet performs the cleanup activities like,

  • Halting the current or background threads
  • Making a recovery list of any related data like cookies to Disk.

After that, the Servlet is badged, ready for the Garbage collector to have it cleared.

The destroy() method Syntax:

public void destroy() {

//destroy() method finalizing

}

Finishing with the life cycle of the Java Servlet, we are now advancing further, where we encounter the Java Servlet features.

Features of java servlets

The Java Servlets carry over the features of Java Programming Language. The key features offered by the Java Servlets are as follows.

  • Portable
  • Efficient
  • Scalable
  • Robust

1. Portable

As discussed above, the servlets feature the same Portable nature as the Java Programming Language. The Servlet program designed in one Operating System’s Platform can be run in a different Operating System Platform with ease.

2. Efficient

The Java Servlet, by its nature, provides an instantaneous response to the client’s request. Also, it can be portable and perform in every environmental condition regardless of the operating system platform.

3. Scalable

We consider Java Servlets to be highly scalable. Servlets use completely lightweight threads for the processes and can simultaneously handle multiple client requests by generating various threads.

4. Robust

The Java Servlets are best known for their robust operating procedures. The servlets extend the features of Java, which include.

  1. Java Security Manager
  2. Java Garbage Collector
  3. Exception Handling.

Implementation of these advanced features makes Java Servlets resilient to any security threats to a decent extent. Also, the garbage collector takes care of memory management and eliminates the issues related to memory management in real-time, leaving Servlets to be Robust.

Followed by the features of Java Servlets, we will dive into the Servlet Request and Response Phases.

--

--