डाउनलोड पी.डी.एफ. ई-बुक्स
JSP - Declaration Tag

JSP Declaration

JSP के declaration tag में variables, methods और classes को declare किया जाता है | अगर किसी भी variable, methhod या class को इस्तेमाल करने से पहले उसे declare किया जाता है |

Syntax for JSP Declaration :

<% declaration %>
OR
<jsp:declaration>
declaration
</jsp:declaration>

Example For Variable Declaration

Source Code :
<html>
<head>
<title>Declaration Tag - Variable Declaration</title>
</head>
<body>

<%! int a = 1; %>
<jsp:declaration>
int b = 2;
</jsp:declaration>

<% 
out.print("Value of a : " + a + "<br />"); 
out.print("Value of b : " + b);
%>

</body>
</html>
Output :
Value of a : 1
Value of b : 2


Example for Method Declaration

Source Code :
<html>
<head>
<title>Declaration Tag - Method Declaration</title>
</head>
<body>

<%! 
String ConcatStr(String str1, String str2){
	return str1 + str2;
}
%>
<% out.print(ConcatStr("Hello", " World")); %>

</body>
</html>
Output :
Hello World


Example for Class Declaration

Source Code :
<html>
<head>
<title>Declaration Tag - Class Declaration</title>
</head>
<body>

<%! 
public class MyClass{
	public String myMethod(){
		return "Hello World";
	}
}
%>
<% 
MyClass c = new MyClass();
out.print(c.myMethod());
%>

</body>
</html>
Output :
Hello World