TopBanner

Content

JavaServer Pages

Servlet technology may be efficient, scaleable, platform independent and buzzword compliant – but it is not practical for all aspects of web applications. Servlets are used for control logic processing, but the view of a web application (frequently HTML, WML, XML or any other markup language) is better created using JavaServer Pages (JSP for short).

A JSP document [that should produce HTML output] looks like a normal HTML document which holds a few extra types of tags. These specific tags fall into six categories:

  1. JSP comments. Text between JSP comment tags will be ignored by the application server, and not sent to the caller. Text between <%-- and --%> is defined as JSP comments.
  2. JSP directives. JSP directives handle definitions that apply to a single JSP page, and are provided within JSP directive delimiters, <%@ and %>
  3. JSP expressions. Text between tags <%= and %> is interpreted as Java code which - after compilation - outputs a String expression to the JSP output stream.
  4. JSP scriptlets. Developers can provide declarations for method and private state members, just like for servlets. Such declarations are given within <%! and %> tags. Java code to be executed at runtime can be given within <% and %> tags.
  5. JSP standard actions. JSP standard actions resemble HTML or other markup tags, and enable interaction with ValueObjects (VOs) or performing include or forward operations from JSP pages.
  6. JSP custom actions. Developers can create custom actions (or Tag Library Descriptors, TLD) which are Java code macros, bound into markup tags. This is a time-consuming but elegant method to provide custom Tags for web developers.

First JSP example

The first JSP example is a HTML page where scriptlets output dynamic data into the result of the HTML response. Note that the Java code within the scriptlet tag does not end with a semicolon. (This is defined by the JSP specification).

<%@ page contentType="text/html; charset=ISO-8859-1" import="java.util.*" %>
<html><body>
  <table border="2">
  <tr>
	<td>Current timestamp</td>
	<td><%= new Date() %></td>
  </tr>
  <tr>
	<td>Java home</td>
	<td><%= System.getProperty("java.home") %></td>
  </tr>
  </table>
</body></html>

The JSP document is automatically converted into a Servlet by the J2EE Application Server. The generated servlet is then compiled, with all properties equal to other servlets. When calling the firstJsp.jsp file from a web browser, the result is what we expected: the content in the JSP expression tags has been replaced by its resulting string output, as shown in the image right.

Altering the source code of the file firstJsp.jsp will make the JSP engine recreate the servlet source code file, and recompile the servlet. This is done automatically by the application server, and changes are picked up regardless of where they were placed within the document (i.e. within JSP scriptlets or text). The change introduced into the JSP document is:

    <td>Java version</td>
    <td><%= System.getProperty("java.version") %></td>