JavaServer Pages, Comments, Declaration, and Expressions
Java Programming, Lecture Notes # 752.
See the Lesson #750 for a description of JSP.
Is JSP difficult to use?
JSP requires you to combine Java programming syntax with special HTML syntax, and consequently, the coding effort can be fraught with details.
While learning the details may not be much fun, you can't write the code without knowing the syntax.
This lesson will provide syntax details (with examples) for
Two kinds of comments
There are two kinds of comments in JSP
- Comments intended for output to the HTML source
- Hidden comments
Note that the syntax of an output comment can contain an optional JSP expression (terms enclosed in square brackets are optional).
Expressions are discussed in detail later in this lesson.
[ <%= expression %> ] -->
Looks sort of like an HTML comment
If you are already familiar with HTML, you will recognize that, except for the ability to include an expression, this is an ordinary HTML comment. However, the inclusion of the expression makes it very special.
The JSP engine returns the comment as part of the HTML output that it sends to the client. The browser doesn't render HTML comments in the standard browser window. Therefore, you must view the HTML source via your browser to see the comment and the output from any embedded expressions.
A sample JSP page
Figure 1 shows a JSP page that contains several output comments, both with and without embedded expressions.
Must view HTML source
Note that in this case, the output (date and time) produced by the expression embedded in the comment is also not visible in the standard browser output window. You must view the HTML source via your browser to see the date and time output from the expression.
The HTML source
Figure 2 shows the HTML source produced by the above JSP page and sent back to the browser.
As you can see, the date and time produced by the expression in the comment appears as part of the corresponding comment in the HTML source.
An expression that is included in a comment is dynamic. It is evaluated when the Web browser first requests the page or reloads the page.
This can be demonstrated using the sample JSP page shown in Figure 1. Loading this page and then reloading the page will cause the time value produced by the expression in the comment to have a different value with each reload.
<%-- hidden comment --%>
Lots of percent characters
Note the use of the percent character (%) in the tag. We will be seeing lots of percent characters in our discussion of JSP.
Sometimes on my screen with a small font, the percent character looks a lot like the joining of two numeric characters, 96, so don't let that confuse you if you experience the same thing.
Hidden comments are ignored
The JSP engine ignores hidden comments. Further, it doesn't process any Java code that is within the hidden comment tags. This makes hidden comments useful for using the age-old technique of commenting out code during debugging.
Figure 1 also contains some hidden comments.
Hidden comments not visible at the browser
Because the JSP engine ignores a hidden comment, it is not sent to the client. Therefore, it is not visible either in the material displayed by the browser in the standard browser window, or in the material that you see when you view the HTML source via your browser.
An examination of Figure 2 shows that the hidden comments did not appear in the HTML source sent to the browser. Instead, there were blank lines where the hidden comments would otherwise appear.
An escape character
Obviously, the body of your comment may not include the following four characters in sequence because the sequence would signal the end of the comment.
If you need to include this sequence of characters in the body of the comment, you will need to use the backslash as an escape character as follows:
The syntax of a JSP declaration is as follows (note the exclamation mark following the percent character on the left):
<%! declarations %>
Here are some examples of JSP declarations:
<%! int a = 100, b = 2; %>
<%! java.util.Date myDate = new java.util.Date(); %>
According to Sun, here are some of the rules governing JSP declarations:
- A declaration must contain at least one complete declarative statement.
- You can declare any number of variables or methods within one declaration tag, provided that they are separated by semicolons.
- The declaration must be valid in the scripting language used in the JSP file. (In JSP 1.0, you must use the Java programming language for scripting, and declarations must conform to the Java Language Specification.)
- You must end the declaration with a semicolon (the same rule as for a Scriptlet, but different from an Expression). (Scriptlets will be discussed in a future lesson.)
- You must declare the variable or method before you use it in the JSP file.
- You can already use variables or methods that are declared in packages imported by the Page Directive, so there is no need to declare them. (The Page Directive will also be discussed in a future lesson.)
A declaration has page scope. It is valid in scriptlets, expressions, and other declarations within the same JSP source file.
A JSP file can include other JSP files using the Include Directive that will be discussed in a future lesson.
If the JSP file does include other files, the rules regarding scope are more complex.
Here are the rules of scope as defined by Sun:
- If the JSP source file does not include any other files, declarations have page scope.
- If the JSP source file includes a dynamic file with <jsp:include>, declarations have page scope. I will discuss <jsp:include> in a future lesson.
- If the JSP source file includes static files with <jsp:include> or the Include Directive, declarations have translation unit scope. A translation unit is the JSP source file plus all of its static include files, but without any dynamic include files.
I will provide a sample JSP page that makes use of declarations after I discuss expressions in the next section.
The syntax for an expression tag is as follows (note the equal character following the percent character on the left):
<%= expression %>
The expression contained within the tag is
- Converted to a String
- Inserted into the HTML text where the expression appears in the JSP file
An expression produces text
You can use an expression as part of the HTML text in a JSP file, because the result of evaluating the expression is always converted to a String.
Some more rules
According to Sun, here are some of the rules that govern the use of expressions:
- You cannot use a semicolon to end an expression in an expression tag (however, the same expression within scriptlet tags requires the semicolon).
- The expression tag can contain any expression that is valid according to the Java Language Specification.
- You can sometimes use expressions as attribute values in JSP tags.
- If an expression has more than one part, the parts are evaluated in left-to-right order as they appear in the tag.
Figure 3 is a JSP page that contains some declarations and some expressions embedded in normal HTML text.
Note that the declarations contain semicolons, and the expressions don't contain semicolons.
Figure 4 shows what my browser displays when I cause this JSP file to be loaded.
As you can see, the result of evaluating each of the expressions is converted to a String and concatenated onto the normal HTML text in which it is embedded.
[ <%= expression %> ] -->
<%-- hidden comment --%>
<%! declarations %>
<%= expression %>
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
About the author
Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.