Scriptlet Directive Expression Declaration
Scriptlet: <% %>
ALL scriptlet and expression code lands in a service method.
That means variables declared in a scriptlet are always LOCAL variables!
<%@ page import=”foo.*” %>
<html>
<body>
The page count is:
<% out.println(Counter.getCount()); %>
</body>
</html>
Directive: <%@ %>
<%@ page import=”foo.*” %>
Expression: <%= %>
NEVER end an expression with a semicolon!
<%@ page import=”foo.*” %>
<html>
<body>
The page count is now:
<%= Counter.getCount() %>
</body>
</html>
Declaration: <%! %>
JSP declarations are for declaring members of the generated servlet class. That means both variables and methods! In other words, anything between the <%! and %> tag is added to the class outside the service method. That means you can declare both static variables and methods.
<html>
<body>
<%! int count=0; %>
<%! int doubleCount() {
count = count*2;
return count;
}
%>
The page count is now:
<%= ++count %>
</body></html>
Here the count variable will be outside the service method.
and doubleCount() method will be also outside of the service method.