JSP Scripting Elements

JSP scripting elements are used to create and access objects, define methods, and manage the flow of control. Since one of the goals of JSP technology is to separate static template data from the code needed to dynamically generate content, very sparing use of JSP scripting is recommended. Some of the work that requires the use of scripts can be eliminated by using custom tags.

JSP technology allows a container to support any scripting language that can call Java objects. If you wish to use a scripting language other than the default, java, you must specify it in a page directive at the beginning of a JSP page:

<%@ page language="scripting language" %> 

Since scripting elements are converted to programming language statements in the JSP page's servlet class, you must declare any classes and packages used by a JSP page. If the page language is java, you declare that a JSP page will use a class or package with the page directive:

<%@ page import="packagename.*, fully_qualified_classname" %> 

For example, bookstore example page showcart.jsp imports the classes needed to implement the shopping cart with the following directive:

<%@ page import="java.util.*, cart.*" %> 

Declarations

A declaration is used to declare variables and methods in a page's scripting language. The syntax for a declaration is:

<%! scripting language declaration %> 

When the scripting language is the Java programming language, variables and methods in JSP declarations become declarations in the JSP page's servlet class.

The bookstore example page initdestroy.jsp defines an instance variable named bookDB and the initialization and finalization methods jspInit and jspDestroy discussed earlier in a declaration:

<%!
	private BookDB bookDB; 

	public void jspInit() {
		...
	}
	public void jspDestroy() {
		...
	}
%> 

Expressions

A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client. When the scripting language is the Java programming language, an expression is transformed into a statement that converts the value of the expression into a String object and inserts it into the implicit out object.

The syntax for an expression is:

<%= scripting language expression %> 

Note that a semicolon is not allowed within a JSP expression, even if the same expression has a semicolon when you use it within a scriptlet.

The following scriptlet retrieves the number of items in a shopping cart:

<%
	// Print a summary of the shopping cart
	int num = cart.getNumberOfItems();
	if (num > 0) {
%> 

Expressions are then used to insert the value of num into the output stream and determine the appropriate string to include after the number:

<font size="+2">You have <%= num %> 
	<%= (num==1 ? " item" : " items") %> in your shopping cart.
</font> 

Scriptlets

A scriptlet is used to contain any code fragment that is valid for the scripting language used in a page. The syntax for a scriptlet is:

<%
	scripting language statements
%> 

When the scripting language is set to java, a scriptlet is transformed into a Java programming language statement fragment and is inserted into the service method of the JSP page's servlet. A programming language variable created within a scriptlet is accessible from anywhere within the JSP page.

showcart.jsp contains a scriptlet that retrieves an iterator from the collection of items maintained by a shopping cart and sets up a construct to loop through all the items in the cart. Inside the loop, the JSP page extracts properties of the book objects and formats them using HTML markup. Since the while loop opens a block, the HTML markup is followed by a scriptlet that closes the block.

<% 
	Iterator i = cart.getItems().iterator();
	while (i.hasNext()) {
		ShoppingCartItem item =
			(ShoppingCartItem)i.next();
		BookDetails bd = (BookDetails)item.getItem();
%>

		<tr> 
		<td align="right" bgcolor="#ffffff"> 
		<%=item.getQuantity()%>
		</td> 
		<td bgcolor="#ffffaa"> 
		<strong><a href="
		<%=request.getContextPath()%>/bookdetails?bookId=
		<%=bd.getBookId()%>"><%=bd.getTitle()%></a></strong> 
		</td> 
		...
<%
	// End of while
	}
%> 

The output appears below:


From the J2EE Tutorial section on JavaServer Pages Technology by Stephanie Bodoff.

Maintained by John Loomis, last updated 31 March 2001