O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

But then Kim mentions “expressions”

Just when you thought it was safe, Kim notices the scriptlet with an out.println() statement. This is JSP, folks. Part of the whole point of JSP is to avoid println()! That’s why there’s a JSP expression element—it automatically prints out whatever you put between the tags.

image with no caption

Scriptlet code:

<%@ page import="foo.*" %>
<html>
<body>
The page count is:
<% out.println(Counter.getCount()); %>
</body>
</html>

Expression code:

<%@ page import="foo.*" %>
<html>
<body>
The page count is now:
<%= Counter.getCount() %>
</body>
</html>

Note

The expression is shorter—we don’t need to explicitly do the print...

Notice what’s different between the tag for the scriptlet code and the tag for the expression? The scriptlet code is between angle brackets with percent signs : <% and %>. But the expression adds an additional character to the start of the element—an equals sign (=).

So far we’ve seen three different JSP element types:

Scriptlet:

<% %>

Directive:

<%@ %>

Expression:

<%= %>

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required