---
description: In this tutorial, we will be learning the basic tags of JSP and how to add comments into JSP. Along with this, we will also create a JSP and run that JSP on the server. JSP Declaration JSP Syntax JSP
title: JSP Elements Tag
image: https://www.guru99.com/images/jsp-elements-tag.png
---

[Skip to content](#main)

**⚡ Smart Summary**

JSP Elements Tag structures allow developers to embed Java directly into a page through declaration, scriptlet, expression, and comment tags, enabling variable creation, logic execution, dynamic output, and readable documentation within a single server-rendered file.

- 📝 **Declaration Tag:** The <%! %> tag declares variables and methods at servlet class level, outside the service method.
- ⚙️ **Scriptlet Tag:** The <% %> tag places Java statements that run inside \_jspservice() on every client request.
- 🔢 **Expression Tag:** The <%= %> tag evaluates an arithmetic or logical expression and writes its result to the output.
- 💬 **Comment Tag:** The <%– –%> tag hides notes from the container and browser, keeping code documented and clean.
- 🚀 **Run on Server:** A complete JSP page is packaged in a WAR file and deployed on a web server such as JBoss.
- 📁 **Directory Structure:** JSP files sit outside WEB-INF, while configuration and library files stay inside the WEB-INF folder.

Read More

![](https://www.guru99.com/images/jsp-elements-tag.png)

## JSP Tags

In this tutorial, we will be learning the basic tags of JSP and how to add comments into JSP. Along with this, we will also create a JSP and run that JSP on the server.

## JSP Declaration

- A declaration tag is a piece of [ Java](https://www.guru99.com/java-tutorial.html) code for declaring variables, methods and classes. If we declare a variable or method inside declaration tag it means that the declaration is made inside the servlet class but outside the service method.
- We can declare a static member, an instance variable (can declare a number or string) and methods inside the declaration tag.

**Syntax of declaration tag:**

```
<%!  Dec var  %>
```

Here Dec var is the method or a variable inside the declaration tag.

**Example:**

In this example, we are going to use the declaration tags

```
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Declaration Tag</title>
</head>
<body>
<%! int count =10; %>
<% out.println("The Number is " +count); %>
</body>
</html>
```

**Explanation the code:**

**Code Line 10:** Here we are using declaration tag for initializing a variable count to 10.

When you execute the above code you get the following output:

[![JSP Declaration]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements2.png)

**Output:**

The variable which is declared in the declaration tag is printed as output.

## JSP Scriptlet

- Scriptlet tag allows to write Java code into JSP file.
- JSP container moves statements in \_jspservice() method while generating servlet from jsp.
- For each request of the client, service method of the JSP gets invoked hence the code inside the Scriptlet executes for every request.
- A Scriptlet contains java code that is executed every time JSP is invoked.

**Syntax of Scriptlet tag:**

```
<% java code %>
```

Here <%%> tags are scriplets tag and within it, we can place java code.

**Example:**

In this example, we are taking Scriptlet tags which enclose java code.

```
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Scriplet</title>
</head>
<body>
<% int num1=10;
   int num2=40;
   int num3 = num1+num2;
   out.println("Scriplet Number is " +num3);
%>
</body>
</html>
```

**Explanation of the code:**

**Code Line 10-14 :** In the Scriptlet tags where we are taking two variables num1 and num2 . Third variable num3 is taken which adds up as num1 and num2.The output is num3.

When you execute the code, you get the following output:

[![JSP Scriptlet]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements4.png)

**Output:**

The output for the Scriptlet Number is 50 which is addition of num1 and num2.

**Don't Miss:**

- [MVC in JSP (Architecture)](https://www.guru99.com/jsp-mvc.html)
- [JSTL (JSP Standard Tag Library)](https://www.guru99.com/jsp-tag-library.html)
- [JSP Client Request](https://www.guru99.com/jsp-client-request.html)
- [JSP Interview Questions (2026)](https://www.guru99.com/jsp-interview-questions.html)

## JSP Expression

- Expression tag evaluates the expression placed in it.
- It accesses the data stored in stored application.
- It allows create expressions like arithmetic and logical.
- It produces scriptless JSP page.

**Syntax:**

```
<%= expression %>
```

Here the expression is the arithmetic or logical expression.

**Example:**

In this example, we are using expression tag

```
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Expression</title>
</head>
<body>
<% out.println("The expression number is "); %>
<% int num1=10; int num2=10; int num3 = 20; %>
<%= num1*num2+num3 %>
</body>
</html>
```

**Explanation of the code:**

**Code Line 12:** Here we are using expression tags where we are using an expression by multiplying two numbers i.e. num1 and num 2 and then adding the third number i.e. num3.

When you execute the above code, you get the following output:

[![JSP Expression]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements6.png)

**Output:**

The expression number is 120 where we are multiplying two numbers num1 and num2 and adding that number with the third number.

## JSP Comments

Comments are the one when JSP container wants to ignore certain texts and statements.

When we want to hide certain content, then we can add that to the comments section.

Syntax:

```
<% -- JSP Comments %>
```

T his tags are used to comment in JSP and ignored by the JSP container.

\<!—comment –>

This is HTML comment which is ignored by browser

**Example:**

In this example, we are using JSP comments

```
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Comments</title>
</head>
<body>
<%-- Guru Comments section --%>
<% out.println("This is comments example"); %>

</body>
</html>
```

**Explanation of the code:**

**Code Line 10:** Here we are adding JSP comments to the code to explain what code has. It is been ignored by the JSP container

When you execute the above code you get the following output:

[![JSP Comments]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements8.png)

**Output:**

We get the output that is printed in println method. Comments are ignored by container

## Creating a simple JSP Page

- A JSP page has an HTML body incorporated with Java code into it
- We are creating a simple JSP page which includes declarations, scriplets, expressions, comments tags in it.

**Example:**

```
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru JSP Example</title>
</head>
<body>

<%-- This is a JSP example with scriplets, comments , expressions --%>
<% out.println("This is guru JSP Example"); %>
<% out.println("The number is "); %>
<%! int num12 = 12; int num32 = 12; %>
<%= num12*num32 %>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</body>
</html>
```

**Explanation of the code:**

**Code Line 1:** Here we are using directives like language, contentType and pageEncoding. Language is Java and content type is text/html with standard charset ISO 8859. Page encoding is standard charset.

**Code Line 11:** Here we are using JSP comments to add comments to the JSP

**Code Line 14:** Here we are declaring variables num12 and num32 initializing with 12.

**Code Line 15:** Here we are using an expression where we are multiplying two numbers num12 and num32.

**Code Line 16:** Here we are fetching today’s date using date object.

When you execute the above code, you get the following output

[![Creating a simple JSP Page]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements10.png)

**Output:**

We are printing overhere,

- This is guru JSP example.
- The number is num12\*num32 (12\*12).
- Today’s date is the current date

## How to run simple JSP Page

- JSP can be run on web servers or application servers.
- Here we will be using a webserver, and we can deploy it on the server enclosing it in a war application.
- We can create JSP in an application (war).

This is an application which has following directory structure, and the application has to be build.

[![Run Simple JSP Page]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements11.png)

This application has to be built, and the following message will appear after the build is successful:

[![Run Simple JSP Page]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements12.png)

After the application is built then, the application has to be run on the server.

To run JSP on the webserver, right click on the project of the IDE ([eclipse](https://www.guru99.com/install-eclipse-java.html) used in this case) and there are many options. Select the option of run on the server. It is shown in the screenshot below;

From the diagram, following points are explained:

1. There are two options either to choose a server or manually add the server to this application. In this case, we have already added JBoss server to the application hence, we select the existing server.
2. Once we select the server the server option is shown in point 2 which server we want to select. There can be multiple servers configured on this application. We can select one server from all those options
3. Once that option is selected click on finish button and application will run on that server.

[![Run Simple JSP Page]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements13.png)

In the below screenshots, you can notice that our JSP program gets executed, and the test application is deployed in [JBoss server](https://www.guru99.com/install-jsp-jboss-server-java.html) marked in the red box.

[![Run Simple JSP Page]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements14.png)

## Directory Structure of JSP

In directory structure, there is a root folder which has folder WEB-INF, which has all configuration files and library files.

JSP files are outside WEB-INF folder

[![Directory Structure of JSP]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements15.png)

**Directory structure of JSP**

**Example:**

In this example there is test application which has folder structure has following:

[![Directory Structure of JSP]()](https://www.guru99.com/images/jsp/022716_0859_JSPElements16.png)

## FAQs

❓ What is the difference between a JSP declaration and a scriptlet?

A declaration tag places variables and methods at the servlet class level, outside the service method. A scriptlet tag places code inside the service method, so it runs on every client request.

🧹 Are JSP scriptlets recommended in modern applications?

No. Scriptlets mix Java with markup and are hard to maintain. Modern JSP pages favor Expression Language, JSTL, and tag libraries, which keep presentation separate from business logic and improve readability.

🔢 Why does a JSP expression tag not use a semicolon?

An expression tag holds a single value that the container converts to a string and writes to the output. Adding a semicolon turns it into a statement, which is invalid inside expression tags.

🤖 How can AI help write JSP elements correctly?

AI can generate declaration, scriptlet, and expression tags from a plain description, flag syntax mistakes, and suggest cleaner alternatives such as JSTL, helping beginners build correct JSP pages faster.

🤖 Can AI convert JSP scriptlets into modern code?

Yes. AI tools can refactor scriptlet-heavy pages into Expression Language and JSTL, reducing embedded Java. Developers should review the output to confirm the logic and scope behavior stay the same.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/jsp-elements-tag.png","url":"https://www.guru99.com/images/jsp-elements-tag.png","width":"700","height":"250","caption":"JSP Elements Tag","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/jsp-elements.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/jsp","name":"JSP"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/jsp-elements.html","name":"JSP Elements Tag"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/jsp-elements.html#webpage","url":"https://www.guru99.com/jsp-elements.html","name":"JSP Elements Tag","dateModified":"2026-10-09T17:17:37+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/jsp-elements-tag.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/jsp-elements.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"JSP","headline":"JSP Elements Tag","description":"In this tutorial, we will be learning the basic tags of JSP and how to add comments into JSP. Along with this, we will also create a JSP and run that JSP on the server. JSP Declaration JSP Syntax JSP","keywords":"jsp","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-09T17:17:37+05:30","image":{"@id":"https://www.guru99.com/images/jsp-elements-tag.png"},"copyrightYear":"2026","name":"JSP Elements Tag","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is the difference between a JSP declaration and a scriptlet?","acceptedAnswer":{"@type":"Answer","text":"A declaration tag places variables and methods at the servlet class level, outside the service method. A scriptlet tag places code inside the service method, so it runs on every client request."}},{"@type":"Question","name":"Are JSP scriptlets recommended in modern applications?","acceptedAnswer":{"@type":"Answer","text":"No. Scriptlets mix Java with markup and are hard to maintain. Modern JSP pages favor Expression Language, JSTL, and tag libraries, which keep presentation separate from business logic and improve readability."}},{"@type":"Question","name":"Why does a JSP expression tag not use a semicolon?","acceptedAnswer":{"@type":"Answer","text":"An expression tag holds a single value that the container converts to a string and writes to the output. Adding a semicolon turns it into a statement, which is invalid inside expression tags."}},{"@type":"Question","name":"How can AI help write JSP elements correctly?","acceptedAnswer":{"@type":"Answer","text":"AI can generate declaration, scriptlet, and expression tags from a plain description, flag syntax mistakes, and suggest cleaner alternatives such as JSTL, helping beginners build correct JSP pages faster."}},{"@type":"Question","name":"Can AI convert JSP scriptlets into modern code?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI tools can refactor scriptlet-heavy pages into Expression Language and JSTL, reducing embedded Java. Developers should review the output to confirm the logic and scope behavior stay the same."}}]}],"@id":"https://www.guru99.com/jsp-elements.html#schema-1128321","isPartOf":{"@id":"https://www.guru99.com/jsp-elements.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/jsp-elements.html#webpage"}}]}
```
