How to Use JSP Declaration Tags

Posted on

This article is specifically written just to show how to use JSP Declaration Tags. As specifically the name refers, declaration tags in JSP is a certain tags used to declare something. Furthermore, in the context of JSP, it is used to declare variables even methods or classes.  JSP file created will be compiled into a class where the rule of variables, methods and classes in Java programming language will be taken as full-consideration.

To be able to declare a variable in JSP, just follow the format shown below :

<%! int i = 1; %>

The above declaration is a declaration tag which is indicated with the exclamation mark (!). The variable which is declared is ‘i’ and it has a primitive data type of integer. Moreover, it is initialized with the value of ‘1’. By declaring the variable in this kind of format, it will be considered as class property or attribute since the JSP file itself is going to be considered as one compiled java class whenever it is being executed in the Java Application Server. To prepare to execute the following example, creating a new JSP file with some declaration tags inside, just follow steps explained in several article written before. Below is another example with the steps taken as follows :

1.  Prepare Java program which can be used.

In order for JSP to be executed, Java program is necessary to be installed in the operating system. The explanation on installing Java program can be viewed in the article titled ‘How to Install Java Program in Ubuntu Linux Operating System’ in this link. If the article doesn’t fit with the current environment used, find other resources in the internet which suits the conditions.

2. Configure the Java program so that it can be used in the environment where the operating system exist.

After successfully installed Java program, read the article titled ‘How to Setup Java Environment for Netbeans in Ubuntu Linux Operating System’ in this link in order for the Java program can be properly executed in the current environment of the operating system.

3. Install NetBeans IDE

In the context of this article, Netbeans is used as the IDE for presenting the example. Just read the article titled ‘How to Install Netbeans in Ubuntu Linux operating system’ in this link to get an alternative reference on installing Netbeans. Since it is solely written for installing Netbeans in Ubuntu Linux operating system, try to find another resource which suitable with the operating system environment. Basically, the installation process is easy as long as every prerequisite has been fullfiled such as the Java program.

4. Create a new JSP file.

So, as the requirement above fullfiled, for an example, below is a complete JSP file which is made to demonstrate further the usage of declaration tags in JSP :

Document : index
Created on : May 19, 2018, 10:24:12 PM
Author : user

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
<%! private String name = “Mike”; %>
private String getName(){
out.println(“My Name is : “+getName());


The above JSP file begins with the declaration of a variable named ‘name’ which has a String type and it is initialized with the value of “Mike”. It is also declared with a method named getName() which is made solely to return the value of the variable named ‘name’. Take a look at the like ‘return’ which means returning the value which is held by variable named ‘name’. The last part is just the part for calling the method for retrieving the value of te variable named ‘name’. So, it will print a string “My Name is : Mike”.

5. Run the JSP file

To see the output, the above file which is created in Netbeans IDE,  just execute through Run File menu as shown below :

How to Use JSP Declaration Tags
How to Use JSP Declaration Tags

The output is shown in the following image :

How to Use JSP Declaration Tags
How to Use JSP Declaration Tags

Leave a Reply