Syntax For JSP Declaratives

By: aathishankaran Printer Friendly Format    

Syntax For JSP Declaratives

               While programming in jsp we have to know about the declaratives of jsp. Here the declaratives is explained for jsp files. For the beginners they should know how to declare the variables in jsp program. In this article its explained

           In jsp program mainly three declaratives are used to embed the jsp code into html.

The three declaratives are:

  • <%@ … %>

 

  • <%! … %>

 

  • <% … %>

 

Syntax of JSP Declaratives is:

  <%!
  //java codes
   %>

JSP Declaratives begins with <%! and ends %> with .We can embed any amount of java code in the JSP Declaratives. Variables and functions defined in the declaratives are class level and can be used anywhere in the JSP page.

Example:

<%@page contentType="text/html" %>

<html>

<body>

<%!
int cnt=0;
private int getCount(){
//increment cnt and return the value
cnt++;
return cnt;
}
%>

<p>Values of Cnt are:</p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

</body>

</html>

This program illustrates how to initialize the variables in jsp program. And the program is shows how to embed the jsp in html program. Here both <@% … %> , <!% … %>, and <% … %> tags are used in this program.



Ask a Question



Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial