GuidesJavaServer Pages, Scriptlets

JavaServer Pages, Scriptlets content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java Programming, Lecture Notes # 754.

May 30, 2000


What is JSP?

With JSP, fragments of Java code embedded into special HTML tags are presented to a JSP engine. The JSP engine automatically creates, compiles, and executes servlets to implement the behavior of the combined HTML and embedded Java code.

Previous lessons have explained the use of the following syntax elements:

  • Output comments
  • Hidden comments
  • Declarations
  • Expressions

This lesson will discuss scriptlets.

What is a Scriptlet?

The scriptlet is the workhorse of JSP.  It is basically some Java code contained within a special JSP tag, embedded in an HTML page.

A scriptlet can contain any number of language statements, variable or method declarations, or expressions.

What is the syntax?

The syntax of a scriptlet is as follows:

<% code fragment %>

What are they good for?

According to Sun, you can do any of the following things using scriptlets:

  • Declare variables or methods to use later in the file (also see declarations in an earlier lesson).
  • Write expressions valid in the page scripting language (also see expressions in an earlier lesson).
  • Use any of the JSP implicit objects or any object declared with a <jsp:useBean> tag.  (This tag will be discussed in detail in a subsequent lesson.)
  • Write any other statement that is valid in the page scripting language (if you use the Java programming language, the statements must conform to the Java Language Specification).

Other stuff

You must write any needed plain text, HTML-encoded text, or other JSP tags outside the scriptlet.

When are scriptlets executed?

Scriptlets are executed when the JSP engine processes the client request.

If the scriptlet produces output, the output is stored in the out object, from which you can display it.

Sample Scriptlets

Some unrelated samples of scriptlets follow:

<% c = a/b; %>

This scriptlet divides the variable a by the variable b and assigns the quotient to c.

<% myDate = new java.util.Date(); %>

This scriptlet instantiates a new object of the Date class and assigns the reference to that object to the variable named myDate.

The two scriptlets shown above are very straightforward.  They consist simply of a simple Java expression embedded inside a scriptlet tag.

Have you noticed anything odd?

Those of you who are familiar with HTML and XML may have noticed something a little odd about this syntax.  In particular, the typical syntax of HTML and XML elements is as follows:

<tagName attribute=attributeValue>
  Element Content

In other words, a typical element in HTML or XML consists of

  • A beginning tag with optional attribute values
  • The element content
  • An ending tag

Some HTML elements don’t require an end tag

Of course, in HTML, there are some elements that don’t require an end tag.  In XML jargon, those elements are not well-formed.

XML has special format for empty elements

In XML, if the element doesn’t contain any content, there is a special format that can be used which, in effect, causes the beginning tag to also serve as the end tag.

However, what we have seen so far regarding JSP doesn’t match any of these.  I won’t bore you with the details, but if this is of interest to you, you might want to pursue the matter further by taking a look at some of my XML articles.

Another sample scriptlet

That brings me to another sample scriptlet.  The following sample consists of two scriptlets.  One forms the beginning of a Java for loop, and the other forms the end of a Java for loop.  The body of the loop consists of a standard HTML break tag <br> and a JSP expression.

<% for (int i=0; i<11; i++) { %>
      <%= i %>
<% }//end for loop %>

More effort required

As you can see from this sample, sometimes a lot more effort is required to write scriptlets than would be required to write the same Java code in a Java application, bean, applet, or servlet.

In addition, if you make an error in your Java code, you don’t get much in the way of helpful compilation error messages to help you correct the problem.  (At least that is true with the JSWDK from Sun that I am currently using.)

Using beans

This will lead us later to the concept of packaging the bulk of our Java code in servlets or Java Beans, and to use JSP to execute them.

The results produced by the beans will be embedded in the HTML-encoded text stream.  In other words, the combined use of JSP, servlets, and Java Beans will give us the best of all three worlds.

Sample JSP Page

Figure 1 is a sample JSP page that contains the three sample scriptlets discussed above, along with some JSP declarations and JSP expressions.

I have highlighted the scriptlets in boldface to make them easier to identify.

Standard Java comments

Note that in addition to using the comment tags discussed in an earlier lesson, it is also acceptable to use standard Java comments inside the scriptlet, as evidenced by the last scriptlet shown in Figure 1.

Figure 2 shows the output produced by loading this JSP page into my Netscape 4.7 browser while running the Sun JSWDK-1.0.1 server.

JSP Syntax Summary

In this and previous lessons, we have learned about the following JSP syntax:

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

Hidden comments:
<%– hidden comment –%>

<%! declarations %>

<%= expression %>

<% code fragment %>

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.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories