Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Enter John Doe

Variables are the bread and butter of every programming language...and JSP has them too. A variable can be thought of as a programming construct used to store both numeric and non-numeric data; this data can then be used in different places in your JSP scriptlets.

JSP supports a number of different variable types: integers, floating point numbers, strings and arrays. Unlike PHP, which can automagically determine variable type based on the data it holds, JSP requires you to explicitly define the type of each variable before using it.

Every variable has a name - in JSP, a variable name is preceded by a keyword indicating the variable type, and must begin with a letter, optionally followed by more letters and numbers. Variable names are case-sensitive, and reserved keywords cannot be used as variable names.

For example, "popeye", "one_two_three" and "bigPirateShip" are all valid variable names, while "byte" and "123" are invalid variable names.

The following example demonstrates how variables can be used in a JSP document.

<html>    
<head>    
</head>    
<body>    
<%!    
// define variables here    
String name = "John Doe";    
%>    
   
<%    
// code comes here    
out.println("My name is " + name );    
%>    
</body>    
</html>

As you can see, we've first defined a variable named "name", set things up so that it will hold string, or character, data, and assigned a value ("John Doe") to it. This value is then used by the println() function to display a message in the HTML page.

My name is John Doe

If you're sharp-eyed, you'll have noticed a slight difference between the two JSP blocks in the example above - the first looks like this:

<%!    
...    
%>

...while the second looks like this:

<%    
...    
%>

The first block, within which the variables are defined, is referred to as the "declaration block"; variables declared within this block are available globally, to each and every scriptlet within that JSP document.

You can also define a variable without assigning a value to it, or assign a value to it at a later stage - for example, the following code snippets are equivalent.

<%    
String name;    
name = "John Doe";    
%>    
   
<%    
String name = "John Doe";    
%>

Putting Two And Two Together

Just as you can create a variable to hold strings, you can create variables of other types too:

  • int - used to store integers
  • char - used to store a single character in Unicode format
  • float and long - used to store floating-point numbers
  • boolean - used to store "true" and "false" values (note that unlike languages like C and PHP, JSP does not recognize 1 => true and 0 => false)

Let's take a simple example that adds two numbers and displays the result.

<html>    
<head>    
</head>    
<body>    
<%!    
int alpha = 45;    
int beta = 34;    
int Sum;    
%>    
<%    
// add the two numbers    
Sum = alpha + beta;    
   
// display the result    
out.println("The sum of " + alpha + " and " + beta + " is " + Sum); %>    
</body>    
</html>

And the output is:

The sum of 45 and 34 is 79

In this case, we've simply defined two variables as integer values, assigned values to them, and added them up to obtain the sum.

In a similar vein, the next example demonstrates adding strings together:

<html>    
<head>    
</head>    
<body>    
<%!    
// define the variables    
String apples = "The lion ";    
String oranges = "roars in anger";    
String fruitBasket;    
%>    
<%    
// print the first two strings    
out.println("<b>The first string is</b>:    
" + apples + "<br>"); out.println("<b>The second string    
is</b>: " + oranges + "<br>");    
   
// concatentate the strings    
fruitBasket = apples + oranges;    
   
// display    
out.println("<b>And the combination is</b>: " + fruitBasket + "<br>Who    
says you can't add apples and oranges?!");    
%> </body> </html>

And the output is:

The first string is: The lion    
The second string is: roars in anger    
And the combination is: The lion roars in anger    
Who says you can't add apples and oranges?!

In this case, the + operator is used to concatenate two strings together, which is then displayed via println().
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links