Home arrow Java arrow Page 3 - JavaServer Pages

JavaServer Pages

If you've taken a look at J2EE and JavaServer Pages technology, and want to examine JSP more closely, you've come to the right place. This article, the first of two parts, is excerpted from chapter three of Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2004; ISBN: 1590593413).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
November 03, 2005
  1. · JavaServer Pages
  2. · Writing JSP Pages
  3. · Scriptlets
  4. · Template Data
  5. · Try It Out: Deploying the Web Application in J2EE
  6. · Try It Out: Deploying the Web Application in Tomcat
  7. · Action Elements

print this article

JavaServer Pages - Scriptlets
(Page 3 of 7 )

Scriptlets contain Java code statements. The code in the scriptlet appears in the translated JSP, but not in the output to the client. Any legal Java code statements can appear within a scriptlet. For example, to repeat the phrase "Hello, World!" ten times in the output page, you could use this scriptlet:

for (int i = 0; i < 10; i++) {
  Hello, World!

As in this code snippet, we can freely interleave Java code and HTML and/or text data. Everything between the scriptlet markers (<% and %> ) is script code; everything outside the markers is template data, which is sent to the client as written. Notice that in the above example the Java code block does not need to begin and end within the same scriptlet element. This allows you complete freedom to mix Java code and HTML elements as needed within the page.

The above example is relatively simple. However, as your application gets more complicated and involved, youll get more and more code mixed in with the HTML and the page will tend to get complicated. In the next chapter, we will see how tag libraries can give the same rich behavior as above, but using only XML tags.

Since scriptlets can contain Java statements, the following is a legal scriptlet:

  Vector v = new Vector();
  // more code...

This looks very similar to the code snippet in the declaration section that preceded this section. This might lead you to wonder what the difference between scriptlets and declarations is, since they appear to be the same. Despite that seeming similarity, they are different in the following ways:

  • Scriptlets cannot be used to define a method; only declarations can be used for that.
  • Variables declared in a declaration are instance variables of the JSP page implementation class. These variables are visible to all other code statements or methods in the page.
  • Variables declared in a scriptlet are local to a method in the JSP page implementation class. They are visible only within their defining code block.



Expressions are used to output the value of a Java expression to the client. For example, this code fragment in a JSP:

  The number of tokens in this statement is <%= countTokens   ("The number of
  tokens in this statement is n") %>.

would result in the text "The number of tokens in this statement is 9." being displayed in the browser. The code snippet above calls the hypothetical countTokens(String) method that was shown in the declaration section previously. To count the number of tokens in the statement, a literal copy of the statement is passed to the method. In the code snippet above, the method call returned an int value, which was printed to the clients browser. Here is the same expression using XML style:

  The number of tokens in this statement is 
     countTokens("The number of tokens in this statement 
  is n")

Any legal Java expression can be used with an expression element. An expression could contain a method call, as shown above, or a literal expression such as '2 + 2', or an expression using Java variables or keywords such as 'v instanceof Vector', or any combination of these. Notice also that because declarations and scriptlets contain Java code, the lines of Java code must be terminated with a semicolon. Expressions, however, will not necessarily be legal code statements (but they will be valid expressions), so they do not need a terminating semicolon.


You can use standard HTML comments within the JSP and those comments will appear in the page received by the client browser. Standard HTML comments have this form:

 <!-- This comment will appear in the client's browser >

You can also include JSP-specific comments that use this syntax:

  <%-- This comment will NOT appear in the client's 
  browser %>

JSP comments will not appear in the page output to the client.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials