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:

<%= %>

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.