Introduction to JSP (original) (raw)

Last Updated : 13 May, 2026

JavaServer Pages (JSP) is a server-side technology that creates dynamic web applications. It allows developers to embed Java code directly into HTML 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.

JSP Architecture

JSP follows a three-layer architecture:

client

JSP Architecture

JSP Elements

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

Expression

Expression 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!" %>

Scriplets

ScripletsTag is used to write Java code inside JSP. The code is placed inside the _jspService() method.

**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:

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:

Declarations

Declarations 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.*" %>

<%! Date theDate = new Date(); Date getDate() { System.out.println("In getDate() method"); return theDate; } %> Hello! The time is now <%= getDate() %>

`

Example of a JSP Web Page

HTML `

A Web Page <% out.println("Hello there!"); %>

`

Steps to Create a JSP Application

Below are the steps to create a JSP application.

Prerequisite

Step 1: Create a Dynamic Web Project

Step 2: Create a JSP File

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

**hello.jsp:

HTML `

Hello! The time is now <%= new java.util.Date() %>

`

**Explanation:

Step 3: Running a Simple JSP Page

  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:8080/JSPDemo/hello.jsp

The JSP file is compiled and executed.

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