Declaration
tag is a block of java code for declaring class wide variables, methods and
classes. Whatever placed inside these tags gets initialized during JSP
initialization phase and has class scope. JSP container keeps this code outside
of the service method (_jspService()) to make them class level variables and
methods.
Syntax of
declaration tag:
<%! field or method declaration %>
Difference between JSP Scriptlet tag and
Declaration tag
Jsp
Scriptlet Tag
|
Jsp
Declaration Tag
|
The jsp scriptlet tag can only declare variables not
methods.
|
The jsp declaration tag can declare variables as well
as methods.
|
The declaration of scriptlet tag is placed inside the
_jspService() method.
|
The declaration of jsp declaration tag is placed
outside the _jspService() method.
|
Example of
JSP declaration tag that declares field
In this example of JSP declaration tag, we are declaring the field
and printing the value of the declared field using the jsp expression tag.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>JSP Declaration tag</title>
</head>
<body>
<!--declaration of name variable.... -->
<%! String name="DashZin"; %>
<%="Welcome to : "+name %>
</body>
</html>
Example of
JSP declaration tag that declares method
In this example of JSP declaration tag, we are defining the method
which returns the cube of given number and calling this method from the jsp
expression tag. But we can also use jsp scriptlet tag to call the declared
method.
<html>
<body>
<%!
int cube(int n){
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
Big data service providers should understand the need of Data, and they should work to build more appropriate services to meet the requirements of their clients.
ReplyDelete