If you've taken a look at J2EE and JavaServer Pages technology, and want to examine JSP more closely, you've come to the right place. This article, the first of two parts, is excerpted from chapter three of Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2004; ISBN: 1590593413).
So, now that weve seen how JSP pages work, lets look at what they contain, and how we go about writing them. Take a look at the following line of code:
<html><body><p>Hello, World!</p></body></html>
Admittedly, this example is not a very good JSP example. However, these HTML tags do form a correct and valid JSP file. You could save the above file as HelloWorld.jsp, install it into a web application, and the server would access it as a JSP resource. The point I want to make is that JSP pages tend to look a lot like HTML pages. To make these pages dynamic, you can embed special tags and Java code in them. You can think of JSP pages as web pages with little bits of Java embedded in them.
The reason the example above is not very good is that it isnt dynamic in any way. If your JSP pages dont contain Java code, you might as well just make them static HTML pages. JSP pages are intended to have dynamic behavior; they are supposed to change in response to specific client requests. You give the page dynamic behavior by embedding Java code into the page.
JSP Elements
You can't just write Java code wherever you want in the page, though. You need some way to tell the server which bits are code, and which bits are regular HTML. To do this, the JSP specification defines HTML-like or XML tags that enclose the code in the JSP. Those tags come in three categories:
Directive elements
Scripting elements
Action elements
The original JSP specification used tag formats for these elements that were not compatible with XML; that is, they were not well-formed according to the XML specification. With the JSP 1.2 specification, alternative XML-compliant versions of all the above tags were introduced. You will see both formats in this book, with the original style referred to as JSP style, and the newer as XML style.
Directive Elements
Directive elements provide information to the JSP container about the page. There are three directives available: page, include, and taglib. We will look at page and include here, deferring discussion of taglib to the next chapter. The page and include directives have these forms:
JSP Style
XML
<%@ page attributes %>
<jsp:directive.page attributes />
<%@ include attributes %>
<jsp:directive.include attributes />
You can find the complete list of attributes and their meanings in the JSP specification, which you can download at http://java.sun.com/products/jsp. Shown below are the attributes you are most likely to be using as you start developing JSP pages:
Directive
Attribute
Description
Page
import
Lists the Java packages to be imported into the page. Just as
with a Java source file, the Java code embedded in a JSP
page must import the packages of the classes used with the
code. Multiple package statements are delimited by
commas, for example import="java.io.*,java.util.*".
session
The valid values are "true" or "false". The default value
is "true". If "true", the page participates in a session; if
"false", then it does not, and cannot access any session
information. Sessions are covered later in the chapter.
isThreadSafe
Whether the page is thread-safe or not. If "true", the
container can use the JSP for multiple concurrent request
threads. The default is "true".
info
An arbitrary string. This can have any value. It is provided
so that the JSP can provide information to a management
tool about its contents, purpose, name, etc.
errorPage
The URL of the web page that should be sent to the client if
an error occurs in a page.
isErrorPage
Whether the current page is an error page. The default is
false.
contentType
Defines the content type of the page. The content type can
appear as a simple type specification, or as a type
specification and a charset. The default value is
"text/html" for JSP-style JSP tags and "text/xml" for
XML-style JSP tags. When including the charset, the syntax
for the attribute is contentType="text/html;charset=
char_set_identifier". Whitespace can follow the
semicolon in the attribute value. Charsets indicate how
written characters are encoded, so that pages can support
languages that use different scripts. Information about
charsets can be found at
http://www.w3.org/TR/REC-html40/charset.html.
pageEncoding
The charset of the current page. The default is ISO-8859-1
(Latin script) for JSP-style and UTF-8 (an 8-bit Unicode
encoding) for XML-style tags.
include
file
The file to be included at the current position in the file.
The included file can be any HTML or JSP page or
fragment of a page. The file is specified using a URI to a file
within the web application.
A single JSP page can have multiple instances of the page directive.
The include directive is used, as stated in the table, to include another page within the current page. This might typically be a standard header or footer, but it can include any content. You would use this when you have standard data that you want to include in multiple JSP pages. The file is included when the page is translated into its Java form. Later we will see a function that allows you to include content at request time.
Scripting Elements
The scripting elements are the elements in the page that include the Java code. There are three subforms of this element: declarations, scriptlets, and expressions. Their forms are:
JSP Style
XML
<%! declaration %>
<jsp:declaration>declaration </jsp:declaration>
<% scriptlet code %>
<jsp:scriptlet>code fragment</jsp:scriptlet>
<%= expression %>
<jsp:expression>expression </jsp:expression>
Declarations
A declaration is used to declare, and optionally define, a Java variable or a method. It works just like any declaration within a Java source code file. The declaration only appears within the translated JSP page, but not in the output to the client. For example, to declare a Vector in your JSP, you would use one of these forms:
<%! Vector v = new Vector(); %> <jsp:declaration>Vector v = new Vector ();</jsp:declaration>
This JSP fragment declares a variable v of type Vector and initializes it by calling the Vector constructor. Any variable you declare within a declaration element becomes an instance variable of the JSP page implementation class, and thus is global to the entire page. Thus, you must take care when initializing variables with a declaration, because instance variables are not thread-safe. By default, the server can send multiple requests to the same page simultaneously. You dont want one thread to change the variable while another thread is using the variable.
You can also declare and define methods within a declaration element:
<%! public int void countTokens(String s) { StringTokenizer st = new StringTokenizer(s); return st.countTokens(); } %> <jsp:declaration> public int countTokens(String s) { StringTokenizer st = new StringTokenizer(s); return st.countTokens(); } </jsp:declaration>
Variables or methods in a declaration element can be called by any other code in the page.