Creating a Local Variable in JSP

In jsp when we have to create a method or variable we usually declare it inside the declaration tag.

Creating a Local Variable in JSP

Creating a Local Variable in JSP

        

In jsp when we have to create a method or variable we usually declare it inside the declaration tag. If we declare it inside the declaration directive then then the scope of the variables will be applicable in the whole page. This works like a instance variable. 

Now consider a situation where we have to declare a variable as local, then we should declare the methods and variables in tag except the declaration directive. In this example we are declaring a variable in the scriptlet directive, so the scope of the variable remains upto the scriptlet directive itself. To print the variable on the browse we will use the out implicit object. 

The code of the program is given below:

 

 

<HTML>
  <HEAD>
    <TITLE>Creating a Variable in Jsp</TITLE>
  </HEAD>
  <BODY>
  
    <H1>Creating a Variable in jsp</H1>
    <%
	 int weeks;
	 weeks = 7;
	 out.println("Days of the weeks = " + weeks);
	 %>
	 <br>
	 <%
	 int months;
	 months = 12;
	 out.println("Number of months = "+ months);
	 %>
  </BODY>
</HTML>

Output of the Program:

Download this example.