JavaServer Page Syntax

Objective

The purpose of this topic is to introduce the basic syntax of a JavaServer Page.

JavaServer Page Elements

Lets take another look at a JSP. As you can see it consists of standard HTML and some Java code which is enclosed between a pair of <% %> tags.

<html>
<head>
<title>JSP Test Page</title>
</head>
<body>

<h1>JSP Test</h1>

<%
 for(int c = 1; c < 10; c++)
 {
   out.println("<P>Iteration " + c + "</P>");
 }
%>

</body>
</html>

To indicate to the server that this file contains script that needs processing it is saved with a .jsp extension.

What kind of Java code are we allowed to write in the JavaServer Page ? Obviously we can write code that will execute some kind of processing, here we have a simple for..loop, but we can also write code that sends information to the browser. In the above script we can see this being done by the out.println() statement. Note how it can contain HTML tags.

The <% and %> tags are known as a Scriptlet element of a JSP. There are various different elements that we can use in a JSP but to begin with we will only look at the following elements:

Expressions

The Expression element contains a Java expression that returns a value. This value is then written to the HTML page. The Expression tag can contain any expression that is valid according to the Java Language Specification. This includes variables, method calls than return values or any object that contains a toString() method.

Syntax

<%= expression %>

Example

Our previous example can also be written as follows.

<html>
<head>
<title>JSP Test Page</title>
</head>
<body>

<h1>JSP Test</h1>

<%
 for(int c = 1; c < 10; c++)
 {
%>
   <p>Iteration <%= c %></p>
<%
 }
%>

</body>
</html>

What the JSP engine does when it translates an Expression is to use the out.print() method to write the contents to the output buffer. So if we have,

  <tr>
    <td><%= recordset.getValue("Col Name") %></td>
  </TR>

then a JSP engine may translate this as the following in the Servlet source file,

  out.print("<tr>")
  out.print("<td>")
  out.print(recordset.getValue("Col Name"))
  out.print("</td>")
  out.print("</tr>")

Script Block

A Script Block defines a chunk of Java code. It must only contain valid Java code and not HTML or other JSP scripting elements.

Syntax

<%
  code fragment
%>

Example

<p>
<%
  int a;

  for(a=1; a<10; a++)
  {
    out.println("<P>pass " + a + "</P>");
  }
%>
</p>

Note that you can tightly mix script blocks and HTML as demonstrated in the following example.

<p>
<%
  if(Math.random() < 0.5)
  {
%>
    Have a nice day
<%
  }
  else
  {
%>
    Have a lousy day
<%
  }
%>
</p>

Typically the <% and %> are split over several lines as in the example above but it is perfectly legitimate to have them on the same line although this can make your page look a little unclear.

Comments

You have various methods to add comments to a JavaServer Page. The first and most obvious is to just use the HTML comment tag:

<!-- some comment -->

You can embed a JSP expression within this comment:

<!--
  This page was compiled on
  <%= (new java.util.Date()).toLocalString() %>
-->

Note, HTML comments are sent to the client but are not displayed by the browser. A user can still view these comments by viewing the source HTML for the page. Another comment is the JSP comment and this comment is not sent to the browser making it more suitable for in line documentation.

<%-- this comment is not sent to the browser -->

Note, all these comments are written within the HTML of the page. The last option available are comments in the Java syntax and these are used within a Script Block,

<%
//This is a Java comment
/* This is also a valid Java comment that
   can be split ove more than one line */
%>

Summary

Workshop - JavaServer Page Syntax

The following workshop looks at writing, testing and debugging a JavaServer Page.

Back Next