Name

Scriptlet

Synopsis

Scriptlets are used to embed scripting code fragments in a page. The content must be a code fragment in the scripting language defined by the page directive. Scriptlet code fragments are combined with code for sending the template data between them to the browser. The combination of all scriptlets in a page must form valid scripting-language statements. All JSP implicit variables are visible in a scripting element.

Syntax 1: In a regular JSP page

<% scripting code fragment %>

Syntax 2: In a JSP Document (a JSP page written in XML syntax)

<jsp.scriptlet> scripting code fragment </jsp:scriptlet>

Attributes

None

Example

                     <% java.util Date clock = new java.util.Date( ) %>
                       
                     <% if (clock.getHours( ) < 12) { %>
  Good morning!
<% } else if (clock.getHours( ) < 17) { %>
  Good day!
<% } else { %>
  Good evening!
<% } 
                     %>

Get JavaServer Pages, Second 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.