Introduction to JSP and Servlet

Sneha Sethi
4 min readFeb 2, 2021

--

Servlet and JSP are used for developing dynamic web applications. You can consider JSP as an alternative to servlet technology. But what is this servlet? Let’s first know about Servlet technology. Whenever a client or user request for accessing a website or a web application which is from the browser, this HTTP request is going to the server and the server responds to that request if this request is static which means the web page is already built and saved on the server. But what if the user requests a dynamic website. Now the server will search for that web page with the help of a web container which is a helper application. Examples of the web container are Tomcat, GlassFish, WebSphere, which provides a web server environment in which Java code can run. This java code is used for completing the user request which is also called a servlet. But how does a servlet work? Let’s go deep dive into servlets. How web works are understood by the following diagram.

Servlet:

A servlet is a java class that is used for extending the capabilities of a server. Servlets are mainly used to extend the applications hosted by web servers, however, they can respond to other types of requests too. We can understand the working of the servlets with the help of MVC architecture. MVC stands for Model View Controller.

MVC Architecture

As shown in the above figure. Servlet works as a controller that reads the implicit HTTP request from the client(browsers) and processes that request with the help of a model class which is a POJO class that provides data from the database and displays the data with the help of JSP (is responsible for view).

Life cycle of a servlet:

There are three methods that define the lifecycle of servlets and are implemented by every servlet are init(), service(), and destroy().

  1. init() method:

Let’s assume a user sent an HTTP request to the webserver with the help of the browser and the web server forwarded that request to the web container. Now the web container invokes this init() method for loading the servlet. This method indicates the servlet instance is instantiated and is about to put into the service method. This method is called only once in the lifecycle of the servlet.

//init() method

public class MyServlet implements Servlet{
public void init(ServletConfig config) throws ServletException {
//initialization code
}
//rest of code
}

2. service() method:

This method is invoked to process the HTTP request, two objects are received by this method as shown in the below program.

// service() method

public class MyServlet implements Servlet{
public void service(ServletRequest res, ServletResponse res)
throws ServletException, IOException {
// request handling code
}
// rest of code
}

a. ServletRequest object used to read the data that has been provided in the HTTP request.

b. ServletResponse object used to generate a response.

3. destroy() method: When all the requests are served and currently running threads completed their tasks this method is called by the servlet container to release all the servlet instances.

JSP(Java Server Pages)

JSP stands for Java Server Pages, is used for developing dynamic web pages. JSP is HTML based code that is the view in the MVC architecture which is responsible for showing the output. It is an advanced version of Servlet. JSP is converted to the servlet by JSP container. JSP can access all the Java APIs, so the servlet code is mapped in JSP with the help of JSP tags. In JSP it is easy to code and write in HTML format. JSP also provides an Exception handling feature, implicit objects are also there which reduces the length of code. The example of the JSP code is shown below.

<html>
<title>Print Numbers 1-10</title>
<head>Numbers 1-10</head><br>
<body>
<%
for (int i = 1; i <= 10; i++) {
out.println (i);
}
%>
</body>
</html>

These are the following JSP tags are available in JSP:

  1. Declaration Tag is used to declaring variables. In servlets, we can say the variable declared outside the service method can be declared in JSP by this tag. The syntax for the declaration tag:
<%! int num=10; %>

2. Scriplet tag is used to add the methods, variables, and expressions. The code for the service method can be included in this tag.

<% java code %>

3. Directive tag is used for importing the packages in the JSP.

The types of JSP directives are:
1.@page
2.@include
3.@taglib
The syntax for directive tag:<%@ page import="java.util.*"%>

4. Expression tag is used for printing the data, all the expression we write in expression tag they will be placed automatically in out.println () method.

<%= java valid expression %>

We have seen how we can implement JSP. Whereas JSP provides too many facilities there are some disadvantages of JSP are also there like it is slower than servlets, it is difficult to debug errors in JSP, and output is also shown in HTML so it lacks some features too.

--

--