Home Jsp Simple-jsp-example Creating a Local Variable in JSP
Questions:Ask|Latest



Creating a Local Variable in JSP
Posted on: March 12, 2008 By Deepak Kumar
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 va

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. 

 

 

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

 


Recommend the tutorial

Ask Questions?    Discuss: Creating a Local Variable in JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments