Abbey Workshop

JSP: JSP Declarations

As the name implies, JSP declarations are used to declare variables. Declarations are enclosed in a <%! %> tag set and end in a semicolon. Here are some examples:

<%! String aString = "When, in the course"; %><%! private int x=1, y=2; %>

Note that multiple variables can be declared in the same declaration statement.

Declaring Methods (v1.1)

In addition to declaring variables, methods can also be declared.

For example, a simple addition method might look like this:

<%! public int sum(int x, int y){
return x+y;
}
%>

Here is some sample code to demonstrate declarations:

Source for jspDeclare.jsp

   1:<html>
   2:<head>
   3:<title>JSP Declarations Page</title>
   4:</head>
   5:<body>
   6:<h2>JSP Declarations Page</h2>
   7:<p>Let's declare some strings and numbers.</p>
   8:<%! String string1 = "When in the course", string2 = " of human events";  %>
   9:<%! int x=1,y=4; %>
  10:Display the strings: <%= string1 + string2 %><br>
  11:The sum of 1 and 4 is: <%= x+y %>
  12:</p>
  13:</body>
  14:</html>