JavaServer Pages, Comments, Declaration, and Expressions

by Richard G. Baldwin
baldwin.richard@iname.com

Java Programming, Lecture Notes # 752

April 18, 2000


Introduction

What is JSP?

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

Syntax and examples for other elements of a JSP page will be provided in future lessons.

Comments

Knowing how to document your code using comments is always important.  Therefore, I will begin with a discussion of comments.

Two kinds of comments

There are two kinds of comments in JSP

Output Comments

The following syntax generates a comment that can be viewed in the HTML file that is produced by the JSP.

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.

<!-- comment 
  [ <%= 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.

Dynamic comments

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 Comments

A hidden comment is used to document the JSP page without the comment being included in the HTML source sent to the client.  The syntax for a hidden comment is as follows.

<%-- 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:

--%\>

Declarations

A JSP declaration is used to declare a variable or a method for use later in the JSP source file.

Declaration syntax

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(); %>

Some rules

According to Sun, here are some of the rules governing JSP declarations:


Scope

A declaration has page scope.  It is valid in scriptlets, expressions, and other declarations within the same JSP source file.

Include directives

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:


I will provide a sample JSP page that makes use of declarations after I discuss expressions in the next section.

Expressions

Syntax

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


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:


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.

JSP Syntax Summary

In this lesson, we have learned about the following JSP syntax:

Output comments:
<!-- comment 
  [ <%= expression %> ] -->

Hidden comments:
<%-- hidden comment --%>

Declarations:
<%! declarations %>

Expressions:
<%= 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.

baldwin.richard@iname.com

-end-