You are on page 1of 6

From this URL: http://www.cs.unc.edu/Courses/jbs/lessons/java/java_jsp/jsp_syntax.

html

JSP Features & Syntax


This lesson provides a brief introduction to JSP features and syntax. While comprehensive, it is
sketchy, discussing and illustrating selected parts of its various components. See the numerous
tutorials available on-line for a more thorough discussion.

As you will recall, JSP pages are processed within a JSP engine -- actually implemented as an
extension of HttpServlet in the Tomcat reference implantation. Within that context, one may
create pages that intermix markup tags, usually HTML, with Java statements or those from
another scripting language. For example, the scriplet

<%
String message = (String) request.getAttribute("errorMessage");
%>

instantiates a String, called message, sets its values to an existing string available in the
environment, and makes it available within the jsp page.

Whereas the Java statements can be conventional Java code, enclosed by special marking tags,
the preferred way to include Java function is through standard or custom tag libraries. A tag
library consists of a set of tags that follow XML conventions and a corresponding set of Java
programs that implement the function designated by the tag. For example, the tag,

<jsp:useBean id="sessionBean" scope="session" class="jbs.databean.SessionBean"/>

provides access in the jsp page to an existing DataBean available in the environment, if such a
bean exists, or creates a new instance of one, if it does not exist.

These constructs are similar, albeit not identical, in function, but the XML form is preferred
since it is more consistent with HTML or similar markup tags, hides the actual Java code that
implements the tag, and is, thus, regarded as easier to read, especially for non-programming
readers.

In the discussions, below, we will look, first, at features that are available through scripting and,
then, those available through tags.

Scripting Elements
Scripting elements allow Java code to be written directly into the JSP page. It is compiled and
available within the page. There are three kinds of scripting elements:
• declarations 
• expressions 
• scriplets 

Declarations

Declarations are used to define variables and methods -- both instance and static -- that are likely
to be repeated. Thus, they correspond to writing code at the Java Class level. They can also be
used to overwrite jspInit or jspDestroy methods that are created when the JSP is compiled.

The basic form is:

<%! Java variable or method %>

Example:

<%! String message; %>

or
<% String message = "Hello, World, from JSP"; %>

Expressions

Expressions are references to variables, methods, or composable structures of such. They are
most often used as a way of dynamically inserting a value into a parameter slot. You may also
think of them as representing a call to a static method. They are evaluated at run time and the
results inserted into the output stream at the location of the expression.

The basic form is:

<%= Java expression %>

Example:

<h2><font color="#AA0000"><%= message%></font></h2>


Note that expressions do not include a semicolon(;).

Scriplets

Scriplets are sections of Java code that are executed in place. They can be as simple as a
declaration of a variable that is treated as an instance variable, but they can also include loops
that mix Java and HTML.

The basic form is:

<% Java code %>

Example:
<% if ( sessionBean.getLoginType() == 0 ) { %>

<p align="center"><B>Super User</B></p>

<%} else if ( sessionBean.getLoginType() == 1 ) {%>

<p align="center"><B>Administrative User</B></p>

<%} else if ( sessionBean.getLoginType() == 2 ) {%>

<p align="center"><B>Author User</B></p>


<%} else {%>

<p align="center"><B>Registered User</B></p>

<%} %>

Implicit Objects

Several local variables are available to scriptlets and expressions through the ServletContext.
They include the following:

• application 
• config 
• session 
• request 
• response 
• out 
• page 
• pageContext 
• exception 

Most are self-explanatory. Application, which is the ServletContext of the JSP and stores
attributes for the duration of the application.. Page is the current jsp page and stores attributes
associated wit the current page. Out is a writer that can be used by scriptlets to put information
onto the output stream.

Directives
Directives are instructions to the JSP compiler. The three types of directives are:

• page  
• include 
• taglib 

Only the include directive will be discussed here.

The include directive instructs the JSP compiler to fetch the file referenced in the directive and
insert it into the page at that point. Thus, it can be used to include standard boilerplate, such as
headers and footers.

The basic form is:

<%@ directive %>

Example:

<%@ include file="header.html" %>

If the included file is HTML, it should not repeat any HTML HEAD or BODY tags that may
already be defined in the output stream.

Run Include JSP

Actions
Actions are JSP tags that transfer control to other server objects or perform operations on other
objects. They may also generate output. They are another means of building view objects within
a M - V - C architecture.

There are some half-dozen action tags. The three that will be discussed here are:

• include 
• forward 
• useBean 

All are marked by tags that have the following form:

<jsp:action attributes />

Include

The include tag is very similar to the include directive, discussed above. It allows content to be
included in place. The content may be either static or dynamic.

Here is an example:
<jsp:include page="header.html" flush="true"/>

Run the JSP

Forward

The forward tag transfer control to a static or dynamic resource, usually referenced by a URL,
that exists on the same server.

The basic form is:

<jsp:forward page="forward_page.html"/>

Example:

<jsp:forward page="forward_page.html" flush="true"/>

Run the JSP

UseBean

The useBean action is by far the most powerful and the most complex of the JSP actions. It
allows a JSP to create an instance or receive an instance of a Java Bean.

The JSP could provide typical servlet controller functions by passing the request object to the
bean and allowing it to extract user parameters and call back-end functions, just like a servlet.
However, supporting those functions in a servlet is generally considered a better practice.
However, rather than transferring result values to another type, such as a Hashtable, and passing
that object to a JSP, if a back-end process returned a result bean back to a controller servlet, it
could simply pass this bean to the relevant JSP.

In fact, so long as the Java server supports it and is properly configured, it permits beans to be
passed from servlet to JSP, to another JSP, to another servlet, etc. This is called chaining and is
an architecture used to provide incrementally processing using filtering principles.

See the lesson on JSP View for an example of the useBean action.

JSP Scopes
All jsp objects exist within a particular scope or context. There are four jsp scopes:

• page 
• request 
• session 
• application 

Request and session should be familiar. Page is the current jsp page. Application is the current
overall application and can include multiple sessions. Thus, all users sharing a given application,
who may be associated with different sessions, would have access to the same set of application-
scope resources.

You might also like