Output through expression

Using an helper Java class for a very simple JSP page is often overkilling. But let's keep things like this just for a moment, to show how to get rid of the direct usage of the println() method on the out object (implicitly defined) using a JSP expression (<%= %>):

The page count is: <%= Counter.getCount() %>
This is actually the same of what we wrote previously:
The page count is: <% out.println(Counter.getCount()); %>
The Java statement in the JSP expression does not have a semicolon at the end, this make sense when you think what really mean the construct: the stuff inside is used as argument for a println() call.

You can find more on JSP expressions in the seventh chapter of Head First Servlet and JSP.

No comments:

Post a Comment