Open In App

Introduction to JSP

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

JavaServer Pages (JSP) is a server-side technology that creates dynamic web applications. It allows developers to embed Java code directly into HTML or XML pages, and it makes web development more efficient.

JSP is an advanced version of Servlets. It provides enhanced capabilities for building scalable and platform-independent web pages.

How is JSP More Advantageous than Servlets?

JSP simplifies web development by combining the strengths of Java with the flexibility of HTML. Some advantages of JSP over Servlets are listed below:

  • JSP code is easier to manage than Servlets as it separates UI and business logic.
  • JSP minimizes the amount of code required for web applications.
  • Generate content dynamically in response to user interactions.
  • It provides access to the complete range of Java APIs for robust application development.
  • JSP is suitable for applications with growing user bases.

Key Features of JSP

  • It is platform-independent; we can write once, run anywhere.
  • It simplifies database interactions for dynamic content.
  • It contains predefined objects like request, response, session, and application, reducing development time.
  • It has built-in mechanisms for exception and error management.
  • It supports custom tags and tag libraries.

JSP Architecture

JSP follows a three-layer architecture:

  • Client Layer: The browser sends a request to the server.
  • Web Server Layer: The server processes the request using a JSP engine.
  • Database/Backend Layer: Interacts with the database and returns the response to the client.

Differences Between JSP and Servlets

Features

JSP

Servlet

Code Length

Jsp required less code

Servlets required more code

Ease of Use

Jsp is simple to use

Servlet is more complex to use

Dynamic Content

Easily embedded in HTML

Requires HTML generation in code

Page Maintenance

Easier to maintain

More challenging

Steps to Create a JSP Application

JSP allows us to embed Java code within HTML pages and making it easy to create dynamic content. Let us start with a simple exercise to convert an existing HTML file into a JSP file.

Steps to Create a JSP

  1. Take any HTML file you have previously created.
  2. Change the file extension from .html to .jsp.
  3. Load the new .jsp file in a browser.

When we load a JSP file for the first time:

  • JSP is converted into a Java file.
  • Java file is compiled into a servlet.
  • Compiled servlet is loaded and executed.

Adding Dynamic Content with JSP

Here is an example to demonstrate how JSP can generate dynamic content:

hello.jsp:

HTML
<!DOCTYPE html>
<html>
<body>
    Hello! The time is now <%= new java.util.Date() %>
</body>
</html>

Explanation:

  • The <%= %> tags enclose a Java expression.
  • new java.util.Date() expression retrieves the current date and time.
  • When the JSP page is loaded in the browser, the Java expression is evaluated at runtime, and output is embedded into the HTML.
  • Each time you reload the page, it displays the current time, demonstrating how JSP dynamically generates HTML content based on Java logic.

JSP Elements

We will learn several elements available in JSP with suitable examples. In JSP elements can be divided into 4 different types.

  •     Expression
  •     Scriplets
  •     Directives
  •     Declarations

1. Expression

This tag is used to output any data on the generated page. These data are automatically converted to a string and printed on the output stream.

Syntax:

<%= "Anything" %>

Note: JSP Expressions start with Syntax of JSP Scriptles are with <%=and ends with %>.  Between these, you can put anything that will convert to the String and that will be displayed.

Example:

<%="HelloWorld!" %>

2. Scriplets

This allows inserting any amount of valid Java code. These codes are placed in the _jspService() method by the JSP engine.

Syntax:

<%

// Java codes

%>

Note: JSP Scriptlets begins with <% and ends %> . We can embed any amount of Java code in the JSP Scriptlets. JSP Engine places these codes in the _jspService() method.

Example:

<%

String name = "Geek";

out.println("Hello, " + name);

%>

Variables available to the JSP Scriptlets are:

  •  Request
  •  Response
  •  Session
  •  Out

3. Directives

A JSP directive starts with <%@ characters. In the directives, we can import packages, define error-handling pages, or configure session information for the JSP page.

Syntax:

<%@ directive attribute="value" %>  

Types of Directives:

  • page: It defines page settings.
  • include: It includes other files.
  • taglib: It declares a custom tag library.

4. Declarations

This is used for defining functions and variables to be used in the JSP.

Syntax:

<%!

//java codes

%>

Note: JSP Declaratives begins with <%! and ends %> with We can embed any amount of java code in the JSP Declaratives. Variables and functions defined in the declaratives are class-level and can be used anywhere on the JSP page. 

Example:

HTML
<%@ page import="java.util.*" %>
<html>
<body>
    <%!
        Date theDate = new Date();
        Date getDate() {
            System.out.println("In getDate() method");
            return theDate;
        }
    %>
    Hello! The time is now <%= getDate() %>
</body>
</html>


Example of a JSP Web Page

Example:

HTML
<!DOCTYPE html>
<html>
<head>
    <title>A Web Page</title>
</head>
<body>
    <% out.println("Hello there!"); %>
</body>
</html>

Running a Simple JSP Page

Steps to Run JSP

  1. Save JSP file using the .jsp extension (e.g., hello.jsp).
  2. Start server (e.g., Apache Tomcat).
  3. Place your application inside the appropriate folder (e.g., webapps for Tomcat).
  4. Open the browser and enter the JSP page URL:

http://localhost:portnumber/YourApplicationContextRoot/jspfile

The JSP file is compiled and executed.

Why Use JSP?

JSP is powerful because it allows us to:

  • Embed Java logic directly into HTML.
  • To create dynamic pages that respond to user actions.
  • To customize content for each user or session.

Next Article
Article Tags :
Practice Tags :

Similar Reads