Nerd For Tech
Published in

Nerd For Tech

JSP and Servlet in Java

In this article, we will learn about JSP and Servlet

Client-Server Architecture

When a client computer submits a data request to the server over the internet, the server accepts the request and returns the digital data required to the client.

Client: A client is a person or an organisation that uses a specific service. A client is a computer in the digital world, capable of receiving information or using a specific service from the service providers.

Server: Similarly, the term “server” refers to a person or medium who serves something. In the digital world, a server is a remote computer that provides data or access to specific services.


A Java programme that runs on a Web server is known as a Servlet. They act as a link between a web browser’s request and the server’s response.

A servlet’s life cycle consists of three primary methods.

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

The init() method: There is only one call to the init method. It’s only called once when the servlet is created, and it’s never called again. It’s used for one-time initializations.

The service() method: The main method for performing the actual task is the service() method. To handle requests from browsers and return responses to them, the servlet container invokes the service() method.

  1. doGet(): This method is called by servlet service method to handle the GET request from the client.
  2. doPost(): This method is called by servlet service method to handle the POST request from the client.

The destroy() method: At the end of a servlet’s life cycle, the destroy() function is only invoked once. This method allows your servlet to conduct cleaning tasks like closing database connections.

Servlet Life Cycle

For example, two numbers are provided by the client and he wants to perform the addition of that two numbers. Let us see how it works internally.


It is the interface that is provided to the client to give two number for addition.


As soon as the client clicks on submit button, it will find which servlet is specified for this request and this will is done in the web.xml file. After that servlet will be called and the processing of the request will start.

After this client will receive a response on his/her browser.

JSP (Java Server Pages)

JSP stands for JavaServer Pages, and it’s a technology for creating dynamic Web pages. This allows developers to embed java code in HTML pages by using specific JSP tags, the majority of which begin with <% and end with % >. The only benefit of JSP is that it is simple to write JSP code for developers.

JSP is internally converted into a servlet.

JSP conversion

Servlets are designed for dynamic web content, after all. However, there is no effective way to represent dynamic web content on a web page using Servlets. There are so many that we need to write down out.print() statements. There can’t be simply one web page in a web application. It’s a collection of web pages, actually. It is not suggested to create web pages in this manner.

Scriptlet tag: Whatever is written into this tag is converted into a service function in the servlet. The business logic is written into this.

<%  java source code %>

Directive tag: This element is used to declare anything that is used by the entire page, such as importing a file.

<%@ directive attribute="value" %>

Declarative tag: This tag is used to declare all variables and statements outside of the service function but within the class.

<%!  field or method declaration %>

Expression tag: Everything inside this tag is printed on the screen.

<%=  statement %>




NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

React Native — Future of cross-platform app development

Remote-DOM aka Virtual Virtual DOM

15 RxJS *Awesome* Tips from 15 Sentinels

Data Driven Form Building in React

Dependency Inversion, Dependency Injection, and Inversion of Control

Create A Successful On-Demand Home Service App

Guide: Setting Up a HNS-Bridge DNS Resolver & HNSD Light Client

How to add custom font in react native easily in 4 steps | Android

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Tomar

Akshay Tomar

More from Medium

Local variables with “var” since Java 10

What Is java Date

Builder design pattern — java

Get Threads Created and Running in Java