JSP and Beyond

a pragmatic primer on building web-based solutions with Java technologies

A Java Server Page Dissected

It is important to remember that a JSP is just HTML code with some special, dynamic markup. The HTML portion of the page is referred to as the template, and the dynamic portions of the page can be scriptlets, directives, expressions, declarations, actions and comments.

The chart below illustrates how the pieces can come together within a single JSP.

Scriptlet <% … %>
Scriptlets let you use full Java code in the body of a JSP. That being said, it is considered bad practice to use scriptlets within the page because they mix application code with presentation code, detracting from many of the available benefits.

The block of code below demonstrates how a series of scriptlets can be used to display a list of months. Notice how the scriptlet can contain HTML within a “for loop” to format how the HTML will be sent to the browser.

String months[] =
{”Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”,
“July”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”};

for(int i = 0; i < months.length; i++ ) {
Month: <%= months[i] %>

Directive <%@ … %>
Directives do not send output to the screen, but set configuration values for the JSP page. There are three main directives: page, taglib and include. Each of these directives and a high level explanation of them is provided below.

The page directive allows you to include code from other packages within the JSP, include another block of JSP code, set the current page to act as an “error” page and set other page level settings.

A common example of a page directive might be <%@ page import=”java.util.*, java.lang.*” %>, which lets a JSP access methods and properties within the Java.util and Java.lang packages more easily. For example, by using the <%@page import=”java.sql.*” %> directive, you are allowed to write code that requires fewer characters to work with in a SQL database. This is somewhat analogous to addresses, and saying that “we are talking about Chicago”, so that any references that are made to a street address would be assumed to be in a Chicago location.

Below are some of the most commonly used page directives.

Directive Description
Import The Import directive easily allows for access to other libraries that you may be using in your project. Some common examples might be
<%@ page import=”java.util.*, java.lang.*” %> or
<%@page import=”java.sql.*” %>
errorPage / isErrorPage
Using the errorPage attribute directive makes it possible to setup a page to handle errors that may occur on that page. The isErrorPage attribute will let the page know that it is capable of being used as the error page that will field errors from other pages.

More information about the complete range of page directives can be found at http://java.sun.com/products/jsp/syntax/1.2/syntaxref1210.html.

The include directive allows a JSP to call another JSP within its same container. This is very helpful to include headers, footers and other repeated elements for a web site or web application. The syntax to include a file called header.jsp would be as follows:

<%@ include file=”/header.jsp” %>.

With the introduction of JSP 1.1, a new feature was added that allows custom tag libraries to be added to JSPs. Using custom tag libraries lets you access rich functionality that allows its complexity to be masked within a particular tag. This is a very useful feature to assist in the separation of logic from presentation, following the JSP best practices, and letting non-developers incorporate tags into HTML templates.

To access a tag library called “mytag” you would use the following line of code within the JSP to make it aware of the custom tag library that was developed.

<%@ taglib uri=”tlds/taglib.tld” prefix=”mytag” %>

The sample below shows how the above line would be incorporated into the JSP and then the mytag prefix used to display content in the middle of the page.

<%@ taglib uri=”tlds/taglib.tld” prefix=”mytag” %>
<title>JSP Custom Tag Sample</title>
< mytag:ourTestTag name=”Stan the JSP Master”/>

Expression <%= … %>
Expressions allow you to directly output values to the page rather than using an out.write statement within a scriptlet. It is a nice, shorthand way of displaying output from a method or variable within your HTML template.

For example, instead of using the following line within a scriptlet or Servlet

out.write(“Bill Smith”);

Youcan use this line to directly write a string value to the page

<%@ page import=”java.util.*” %>
<%! String sName = “Bill Smith”; %>
Welcome <%= sName %>!

Declaration <%! … %>
Declarations are used to set variables and define methods within the JSP. The following example shows a method written inline within the JSP.

<%@ page import=”java.util.*” %>
// This is the method that will return the current time
String GetCurrentTime()
Date date = new Date();
return date.toString();

What time is it? <%= GetCurrentTime() %>

Results of the above code

Actions are XML-based tags that allow access to common pieces of JSP functionality. useBean, setProperty, getProperty.include, forward and plugin are all accessed through user friendly XML-based tags. Think of it as new HTML markup that allows for dynamic, rich functionality in a page with no development by a page designer.

Action Description
Allows simplified interaction with JavaBeans. The useBean action will instantiate a bean if it has not been instantiated already, and associates a scope with the bean.

This XML snippet allows tag-based access to a Bean’s property.

This XML snippet provides a tag-based method to set the value of a Bean’s property.

Merges content from another JSP into the page markup.

Sends the page processing to another JSP or Servlet.

Identifies a value to be sent to another JSP or Servlet that a request is being forwarded to.

The plugin action allows HTML markup to be sent to the user’s browser for Applets based on the platform they are using.

Comment <%– … –%>
Sometimes you only want your comments to show to your development team. NormallyHTML comments (<!—your informative or witty comment here –>) are sent to the requestor’s browser. With the JSP style you can keep comments inline with the code, but they will not be sent with the HTML response to the requestor’s browser.