JSP | Declaration Tag (original) (raw)

Last Updated : 14 May, 2026

The Declaration Tag in JSP is used to declare variables, methods, and classes in a JSP page. The code written inside this tag is placed outside the _jspService() method by the JSP container, making it available throughout the JSP page.

**Syntax:

<%! declaration code %>

**Example: JSP Declaration Tag which initializes a string

HTML `

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>

GeeksforGeeks <%! String username="Geeks"; %>

<%="Hello : "+username %>

`

**Output:

Geeks

output

**Explanation:

**Example : JSP Declaration Tag which initializes a method

HTML `

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

GeeksforGeeks <%! int factorial(int n) { if (n == 0) return 1; return n*factorial(n-1); } %> <%= "Factorial of 5 is:"+factorial(5) %>

`

**Output

Factorial

Output

**Explanation: