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

We need another JSP element...

Declaring the count variable in a scriptlet meant that the variable was reinitialized each time the service method ran. Which means it was reset to 0 with each request. We need to somehow make count an instance variable.

image with no caption

So far we’ve looked at directives, scriptlets, and expressions. Directives are for special instructions to the Container, scriptlets are just plain old Java that lands as-is within the generated servlet’s service method, and the result of an expression always becomes the argument to a print() method.

But there’s another JSP element called a declaration.

image with no caption

JSP declarations are for declaring members of the generated servlet class. That means both variables and methods! In other words, anything between the <%! and %> tag is added to the class outside the service method. That means you can declare both static variables and methods.

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