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
Loading editor…

Expression Examples

Here are valid expression examples:

Example03
Loading editor…

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
Loading editor…

II) Scriptlet

Scriptlets let you embed any Java code directly.

Syntax:

Example05
Loading editor…

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
Loading editor…

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
Loading editor…

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
Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.