10.3. JSP Scriptlets

If you want to do something more complex than insert a simple expression, JSP scriptlets let you insert arbitrary code into the servlet's _jspService method (which is called by service). Scriptlets have the following form:

<% Java Code %>

Scriptlets have access to the same automatically defined variables as expressions (request, response, session, out, etc.; see Section 10.5). So, for example, if you want output to appear in the resultant page, you would use the out variable, as in the following example.

String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);

In this particular instance, you could have accomplished the same effect more easily by using the following JSP expression: ...

Get Core Servlets and JavaServer Pages™ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.