Nearby lessons
5 of 30JSP - Scripting Elements
- Know the four types of scripting elements
- Understand where each type is placed in the generated servlet
- Master the page directive and all its attributes
Scripting Elements allow you to embed Java code directly into a JSP page. This lesson covers expressions, scriptlets, declarations, comments, and all page directive attributes — with complete code examples.
Overview of Scripting Elements
There are 4 types of scripting elements:
| Type | Syntax | Ends with ; | Code placement |
|---|---|---|---|
| Expression | <%= X %> | No | Inside _jspService() as out.print(X) |
| Scriptlet | <% Java Code %> | Yes | Inside _jspService() as raw Java code |
| Declaration | <%! Declarations %> | Yes | Outside _jspService() as class members |
| Comments | <%-- JSP Comment --%> | N/A | Not included in generated servlet |
I) Expression
An expression prints a Java value to the JSP output.
Syntax: <%= expression %>
The equivalent generated servlet code is:
Expression Examples
Here are valid expression examples:
Expression Rules
- No semicolons inside expressions — otherwise you get a 500 error.
E.g.<%= new java.util.Date(); %>is invalid because it generatesout.print(new java.util.Date();); - Void methods not allowed — methods like
clear()have no return value to print. - No space between
%and=—<% =10 %>is invalid (treated as a scriptlet, not an expression). - No declarations inside expressions —
<%= String s = "durga" %>is invalid.
II) Scriptlet
Scriptlets let you embed any Java code directly.
Syntax:
Scriptlet Rules
- Java code inside a scriptlet is placed directly inside
_jspService()of the generated servlet. - Every Java statement must end with
;. - Since scriptlet code lands inside
_jspService(), all JSP implicit objects are available (out, request, session, etc.).
Hit Count Example
A common mistake: using a scriptlet (local variable) for a hit count. The variable resets every request:
III) Declaration Tag
Declarations define instance variables, static variables, methods, and blocks that become members of the generated servlet class — outside of _jspService().
Syntax:
Declaration Rules
- Every statement inside a declaration must end with
;. - You cannot use JSP implicit objects inside declarations — because declarations land outside
_jspService()where those objects are not in scope.
Where Declarations Land
JSP implicit objects are local variables of _jspService(). Declarations are placed outside of that method, so they cannot access implicit objects.
However, scriptlets and expressions land inside _jspService(), so they can use implicit objects.
IV) JSP Comments
JSP supports 3 types of comments:
| Comment Type | Syntax | Visible in Servlet Source | Visible in Browser Response |
|---|---|---|---|
| JSP Comment (recommended) | <%-- comment --%> | No | No |
| HTML Comment | <!-- comment --> | Yes (as template text) | Yes (visible in page source) |
| Java Comment | // or /* */ or /** */ | Yes (inside Java code) | No |
Important: You cannot nest one scripting element inside another (e.g. an expression inside a scriptlet). This causes a compile-time error.
- Expressions land inside _jspService() as print() calls
- Scriptlets land inside _jspService() as raw Java code
- Declarations land outside _jspService() as class members
- The page directive controls imports, session, EL, thread safety, and more