Declaring Methods and Variables with <%! %>

So far, you have seen how to insert Java statements and Java expressions into your code. In case you haven't realized it yet, all the code within the <% %> tags and all the expressions within the <%= %> tags belong to one big Java method in the generated servlet. That is why you can use the <%= %> tags to display a variable that was declared inside the <% %> tags.

You might want to put an entire Java method into a JSP. If you try to declare a method within the <% %> tags, the Java compiler reports an error. It won't let you declare a method within another method.

Use the <%! %> tags to enclose any declarations that belong outside the big method that generates the page. Listing 14.8 shows a JSP file ...

Get Special Edition Using Java™ 2 Enterprise 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.