Table of contents
1.
Introduction
2.
What is JSP
2.1.
Prerequisite
2.2.
Example of a JSP Web Page
3.
Features of JSP
4.
JSP Architecture
4.1.
Key Components of JSP Architecture:
4.2.
JSP Request-Response Cycle
5.
Steps to Create a JSP Application
5.1.
Steps
5.2.
Example Directory Structure for JSP Application:
5.3.
Common Tools for JSP Development
6.
HTTP Protocols
7.
Lifecycle of a JSP Page
7.1.
Phases of the JSP Lifecycle
8.
Advantages and Disadvantages of JSP
8.1.
Advantages of JSP
8.2.
Disadvantages of JSP
9.
JSP v/s Servlet
10.
Frequently Asked Questions
10.1.
Why is JSP used?
10.2.
Is JSP backend or frontend?
10.3.
Is JSP better than HTML?
10.4.
Which language is used in JSP?
11.
Conclusion
Last Updated: Apr 1, 2025
Medium

Introduction to JSP

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JSP stands for Java Server Pages. JSP is a server-side programming language that combines HTML, XML, Java Servlet, and JavaBeans technologies into one highly productive technology. It allows developers to develop reliable, high-performance, platform-independent web applications, and dynamic websites. It has access to the entire family of Java APIs and can be considered an extension of the Servlet. 

Introduction to JSP

JSP provides more functionality than servlets such as JSTL, expression language, custom tags and can also incorporate java code within HTML code with the help of JSP tags. Pages created using JSP are easier to manage as it enables us to separate designing and development. JSP file is stored with.JSP extension.

What is JSP

JavaServer Pages (JSP) is a server-side technology that allows developers to create dynamic web pages by embedding Java code within HTML. JSP files are compiled into servlets by the web server and executed to generate dynamic content. JSP simplifies web development by separating presentation from logic, supports tag libraries, and seamlessly integrates with other Java technologies like servlets and JavaBeans.

Prerequisite

It will be beneficial to have a basic understanding of HTML, CSS, or any programming language along with the below-mentioned technologies before diving into JSP:

  • JSP relies on HTTP protocols to provide client-server communication over the network.
  • JSP is built on the top of the servlet technology to ease the development task for developers.

 

Syntax of JSP:

<html>
   <head><title>Hello World</title></head>
   <body>
      Hello World!<br/>
      <%
         out.println("Welcome to JSP");
      %>
   </body>
</html>


Output:

Example of a JSP Web Page

JavaServer Pages (JSP) is a technology used for developing dynamic, data-driven web pages in Java. JSP allows embedding Java code within HTML, providing a powerful way to create web applications.

Here’s a basic example of a JSP web page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"%>
<%@ page import="java.util.Date" %>
<html>
<head>
    <title>Simple JSP Example</title>
</head>
<body>
    <h1>Welcome to JSP Example Page</h1>
    
    <p>Today's Date and Time: <%= new Date() %></p>
    
    <form action="process.jsp" method="post">
        <label for="username">Enter your name:</label>
        <input type="text" id="username" name="username">
        <input type="submit" value="Submit">
    </form>
    
    <p>
        <% 
            String username = request.getParameter("username");
            if (username != null) {
                out.println("Hello, " + username + "!");
            }
        %>
    </p>
</body>
</html>

Explanation:

  • JSP Directives and Declarations:

<%@ page language="java" ... %>: Specifies the language (Java) and content type for the page.

<%@ page import="java.util.Date" %>: Imports the Date class to get the current date and time.
 

  • Embedding Java in HTML:

<%= new Date() %>: This is a scriptlet that executes the Java code (new Date()) and inserts the current date and time in the web page.
 

  • Form Handling:

The form uses the POST method to send the data to another JSP file (process.jsp).

It includes a text input field (username) to collect the user's name.
 

  • Dynamic Content with Request Parameters:

The second <% %> scriptlet checks if the username parameter was passed from the form.

If the user entered a name, it is displayed as a greeting: "Hello, [username]!".

Features of JSP

  • It helps in reducing the length of the code by allowing the use of multiple tags, e.g., custom tags, action tags, etc.
     
  • It is easier to connect websites to databases.
     
  • It allows reading/writing data easily in the database.
     
  • It is easy to code using JSP as it allows the insertion of Java code into HTML tags.
     
  • It helps in developing dynamic web applications.
     
  • It is secure, browser, and server-independent, so it doesn’t require recompilation.
     
  • It is portable, powerful, flexible, and easy to maintain.

JSP Architecture

JSP (JavaServer Pages) follows a request-response model, where the server processes a request, executes Java code embedded in JSP, and generates dynamic content. The architecture involves several components that work together to handle HTTP requests and responses.

Key Components of JSP Architecture:

  • Client: The user’s web browser sends a request (HTTP request) to the server, typically for a JSP page.
     
  • Web Server: The server receives the request, processes it, and sends it to the JSP container. The web server acts as the intermediary between the client and the server.
     
  • JSP Container (Servlet Container): This is the core component of the JSP architecture. It:

Translates JSP pages into servlets (Java code) for execution.

Manages the lifecycle of JSPs and handles requests.

Passes data between the client and the JSP pages.

Provides session management for maintaining state.

  • Servlet: A servlet is a Java class responsible for handling HTTP requests and responses. JSPs are converted into servlets during runtime by the JSP container. Servlets process dynamic content and interact with data sources like databases.
     
  • JSP Page: The JSP page is a combination of HTML and Java code. The JSP container compiles the JSP page into a servlet, which is executed to generate dynamic content.
     
  • Database/Other Resources: Often, JSP applications will need to interact with a database or other resources to provide data for dynamic web pages.

JSP Request-Response Cycle

  • The client (browser) sends an HTTP request to the server for a specific JSP page.
     
  • The JSP container processes the request, compiles the JSP file into a servlet (if not already done), and executes it.
     
  • The servlet generates dynamic content (HTML, data, etc.) based on Java code and sends it back to the client as an HTTP response.
     
  • The response is rendered on the client’s browser.

Steps to Create a JSP Application

Creating a JSP application involves several steps to set up your development environment, create JSP files, and configure the server.

Steps

  • Set Up Development Environment:

Install Java Development Kit (JDK): Ensure that Java is installed on your system.
 

Install a Web Server: You need a web server that supports JSP, such as Apache Tomcat, Jetty, or Glassfish.
 

Set up an IDE: Use an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, or NetBeans for efficient development.
 

  • Create a Dynamic Web Project:

In your IDE, create a new dynamic web project or servlet project.
 

Select the server runtime (e.g., Apache Tomcat) for deployment.
 

  • Create JSP Files:

In the created project, add a new JSP file (with a .jsp extension).
 

Write HTML and Java code embedded in the JSP file. For example:
 

  • Configure Web Application:

Edit the web.xml deployment descriptor if needed (e.g., to define servlet mappings, session configuration).
 

The web.xml file can also be used to map URLs to specific servlets or JSP pages.
 

  • Compile and Deploy:

Deploy the JSP application on the server (e.g., Apache Tomcat). In your IDE, select the option to run or deploy to the selected server.
 

  • Test the Application:

Open a web browser and access the JSP page by navigating to the URL corresponding to the page (e.g., http://localhost:8080/yourAppName/index.jsp).
 

Ensure that the application runs and generates the expected output.
 

  • Add More Functionality:

As needed, you can add servlets for handling complex business logic, use JavaBeans for encapsulating data, and interact with databases for dynamic content.

Example Directory Structure for JSP Application:

/project-name
    /WEB-INF
        /lib (for external libraries like JDBC drivers)
        web.xml (deployment descriptor)
    /jsp (folder to store JSP files)
        index.jsp (your JSP page)
    /images (folder to store image files)
    /css (folder to store CSS files)

Common Tools for JSP Development

  • Apache Tomcat: A popular servlet container to deploy JSP applications.
     
  • Eclipse IDE with Dynamic Web Plugin: Simplifies Java EE development, including JSP.
     
  • MySQL or PostgreSQL: For database-driven JSP applications.

HTTP Protocols

JSP uses a client-server model, in which a client makes a request to the server and gets a response accordingly. The protocol that the web server/browser uses to communicate is the HTTP protocol, which works based on the request/response model.

used HTTP commands are HEAD, GET, PUT, POST, DELETE, TRACE, OPTIONS, and CONNECT.

 

Source: Link

The HTTP protocol is stateless. It means that no information/state is carried over between subsequent requests from a client. To overcome the stateless HTTP protocol, the server uses sessions to keep information between two subsequent requests of the same client.

Lifecycle of a JSP Page

The lifecycle of a JSP (JavaServer Pages) page consists of several phases from its creation to destruction, allowing dynamic content to be generated based on user requests. Understanding this lifecycle is crucial for developers working with JSP to create efficient and maintainable web applications. Let's discuss the different phases in detail:

Phases of the JSP Lifecycle

Translation: The JSP engine translates the JSP page into a servlet. During this phase, the JSP syntax is converted into Java code.

Compilation: The translated Java servlet code is compiled into bytecode that the JVM (Java Virtual Machine) can execute.

Initialization: When the servlet is first loaded (before it handles any requests), the init() method is called. This method is typically used for one-time startup processes such as resource allocation.

Execution: The service() method is invoked each time the server receives a request for the JSP page. This method generates the dynamic page content based on the request parameters and can be called multiple times throughout the lifecycle of the servlet.

Cleanup: When the servlet is about to be removed from use (typically when the server shuts down or the application is redeployed), the destroy() method is called. This method is used for cleanup activities, such as releasing resources.

Advantages and Disadvantages of JSP

Advantages of JSP

  • JSP supports all the features of servlet technology. It also has better performance over other technologies available.
  • It inherits the features of java technology such as multithreading, exception handling, database connectivity, etc.
  • It enables the separation of content presentation from content generation, making it easier for developers to manage the application.
  • It is highly scalable and provides flexibility in building dynamic web applications.
  • It is built over java. Hence it is platform-independent.
  • Its code is easy to write and maintain.
  • There is no need for recompilation and redeployment in JSP.

Know Why is Java Platform Independent here.

Disadvantages of JSP

  • The output of JSP is in HTML, which lacks advanced features.
  • It is difficult to debug.

JSP v/s Servlet

Let’s have a look at the key differences between JSP and Servlet:

S.NoJSPServlet
1It embeds Java code inside HTML using JSP tags.It is a Java program that supports HTML tags.
2Web developers frequently use it to develop dynamic web applications.They are created and maintained by Java developers.
3It is used for developing the presentation layer of a web application.It is used for developing the business layer of a web application.
4It is possible to access the JSP pages directly. Hence, it doesn’t require web.xml mapping.It isn’t possible to access the pages directly. Hence, web.xml mapping is required.

You can also read about the JSP Architecture and Lifecycle.

Frequently Asked Questions

Why is JSP used?

JSP is used to create dynamic web pages by embedding Java code in HTML, enabling server-side processing and interaction with databases.

Is JSP backend or frontend?

JSP is a backend technology that processes requests on the server, generates dynamic content, and sends the response to the client as HTML.

Is JSP better than HTML?

JSP is more powerful than HTML as it supports dynamic content generation, database interaction, and server-side logic, while HTML is static.

Which language is used in JSP?

JSP uses Java as the programming language, allowing integration with Java classes, servlets, and database connectivity for dynamic web applications.

Conclusion

This blog discussed a basic introduction to JSP, including its important features and properties. JSP is a server-side scripting language used for creating dynamic web pages using java. It allows converting any HTML file to a JSP file by simply changing the file extension. JSP comes with many advanced features that help create and maintain a highly scalable web application.

Live masterclass