Nearby lessons

5 of 30

JSP - Scripting Elements

📌 What You Will Learn
  • 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:

TypeSyntaxEnds with ;Code placement
Expression<%= X %>NoInside _jspService() as out.print(X)
Scriptlet<% Java Code %>YesInside _jspService() as raw Java code
Declaration<%! Declarations %>YesOutside _jspService() as class members
Comments<%-- JSP Comment --%>N/ANot included in generated servlet

I) Expression

An expression prints a Java value to the JSP output.

Syntax: <%= expression %>

The equivalent generated servlet code is:

Example02
JCode Cell
1 
2out.print(expression); // inside _jspService()
3

Expression Examples

Here are valid expression examples:

Example03
JCode Cell
1 
2<%= 27 %> <%= Math.random() %>
3<%= "27" %> <%= 10 * 20 %>
4<%= 10 > 20 %> <%= new Student() %>
5

Expression Rules

  • No semicolons inside expressions — otherwise you get a 500 error.
    E.g. <%= new java.util.Date(); %> is invalid because it generates out.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.
Example04
JCode Cell
1 
2<%= new java.util.ArrayList().size() %> ← valid (has return value)
3<%= new java.util.ArrayList().clear() %> ← invalid (void method)
4

II) Scriptlet

Scriptlets let you embed any Java code directly.

Syntax:

Example05
JCode Cell
1 
2<%
3 // any Java code here
4 out.println("Hello");
5%>
6

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:

Example07
JCode Cell
1 
2<%-- scriptlet version --%>
3<h1>
4<%
5 int count = 0;
6 count++;
7 out.println(count); ← always prints 1 (local variable)
8%>
9</h1>
10 
11<%-- declaration version --%>
12<h1>
13<%!
14 int count = 0; ← instance variable
15%>
16<%
17 count++;
18 out.println(count); ← prints 1, 2, 3... (persists across requests)
19%>
20</h1>
21

III) Declaration Tag

Declarations define instance variables, static variables, methods, and blocks that become members of the generated servlet class — outside of _jspService().

Syntax:

Example08
JCode Cell
1 
2<%!
3 int x = 10; ← instance variable
4 static int y = 20; ← static variable
5 int[] a = {10, 20, 30}; ← instance variable
6 public void m1() {} ← method
7%>
8

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.
Example09
JCode Cell
1 
2<%!
3 public void m1() {
4 out.println("Hello"); ← CE: out cannot be resolved
5 }
6%>
7

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 TypeSyntaxVisible in Servlet SourceVisible in Browser Response
JSP Comment (recommended)<%-- comment --%>NoNo
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.

📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3