Scriptlets

A Java Server Page (JSP) is an HTML file that may contain JSP components. JSP components include:

Scriptlets
Actions
Directives

A scriptlet is an element of the form:

<jsp:scriptlet> PARTIAL_STATEMENT </jsp:scriptlet>
<jsp:expression> EXPRESSION </jsp:expression>
<jsp:declaration> DECLARATION* </jsp:declaration>
<jsp:comment> COMMENT </jsp:comment>

Here's the equivalent friendly syntax:

<% PARTIAL_STATEMENT %>
<%= EXPRESSION %>
<%! DECLARATION* %>
<%-- COMMENT --%>

Example: date.jsp

<%!
   // this is a declaration scriptlet:
   public java.util.Date getDate() {
      return new java.util.Date();
   }
%>


<html>
<head><title> Dates </title></head>
<body>

<!-- this is a statement scriptlet: -->
The date is:
<% out.println(new java.util.Date()); %>
<br />
<%-- these are expression scriptlets: --%>
The date is:
<%= new java.util.Date() %>
<br />
The date is:
<%= getDate() %>

</body>
</html>

Deployment
Request myapp/date.jsp from localhost:8080

Example: flow.jsp

<%! int MAX = 6; %>
<html>
<head><title> Control Flow </title></head>
<body>

<% for (int size = 1; size < MAX; size++) { %>
   <font size = "<%= size %>">
      ABCDEFGHIJKLMNOPQRSTUVWXYZ <br />
   </font>
<% } %>
  

</body>
</html>

Deploying and Testing flow.jsp

Example: craps.jsp

<%!
   java.util.Random generator
      = new java.util.Random();
%>

<html>
<head><title> Craps </title></head>
<body>

<%
   int d1 = generator.nextInt(6) + 1;
   int d2 = generator.nextInt(6) + 1;
   int sum = d1 + d2;
%>

You rolled a <%= d1 %> + <%= d2 %> = <%= sum %>. <br />
 
<% switch(sum) {
   case 2: case 3: case 12: %>
      You lose!
     <% break;
   case 7: case 11: %>
      You win!
      <% break;
   case 4: case 6: case 8: case 10: %>
      Even, roll again.
      <% break;
   default: %>
      Odd, roll again.
<%}%>

</body>
</html>

Warning: Avoid Adjacent Scriplets

The following jsp fragment looks better than our version, but won't compile.

<% switch(sum) { %>
   <% case 2: case 3: case 12: %>
      You lose!
      <% break; %>
   <% case 7: case 11: %>
      You win!
      <% break; %>
   <% case 4: case 6: case 8: case 10: %>
      Even, roll again.
      <% break; %>
   <% default: %>
      Odd, roll again.
      <% break; %>
<% } %>

The problem is the white space formatting between the end of one scriptlet tag and the beginning of the next tag:

   <% break %>
<%
case ... %>

The JSP-to-Java translator translates this as:

break;
out.write("\n");
case ...

The compiler then complains that the middle statement can't be reached, because it is between a break statement and a case clause.

Using if/else Scriptlets

Of course the switch scriptlets can be replaced with the following if/else scriptlets:

<% if (sum == 2 || sum == 3 || sum == 12) %>
   You lose!
<% else if (sum == 7 || sum == 11) %>
   You win!
<% else if (sum == 4 || sum == 6 || sum == 8 || sum == 10) %>
   Even, roll again.
<% else %>
   Odd, roll again.

Deploying and Testing craps.jsp

Example: log.jsp

<%!
   class BadInputException extends Exception {
      public BadInputException(double x) {
         super("bad input = " + x);
      }
   }
 
   public double log(double x) throws BadInputException {
      if (x <= 0) throw new BadInputException(x);
      return Math.log(x);
   }
%>

<html>
<head><title> Log Test </title></head>
<body>
<%
   double result = 0;
   try {
      result = log(1); %>
      Result = <%= result %> <br/>
<% result = log(Math.E); %>
      Result = <%= result %> <br/>
<% result = log(Math.E * Math.E); %>
      Result = <%= result %> <br/>
<% result = log(0); %>
      Result = <%= result %> <br/>
<% } catch (BadInputException e) { %>
      Bad input detected: <%= e.getMessage() %>
<% } %>
</body>
</html>

Deploying and Testing log.jsp