Directives, scriptlets, and expressions declaration JSPs use several delimiters for
scripting functions. The most basic is , which encloses a JSP
scriptlet. A scriptlet is a fragment of Java code that runs when the user requests the page. Other common delimiters include for
expressions, where the scriptlet and delimiters are replaced with the result of evaluating the expression, and
directives, denoted with . Java code is not required to be complete or self-contained within a single scriptlet block. It can straddle markup content, provided that the page as a whole is syntactically correct. For example, any Java
if/for/while blocks opened in one scriptlet must be correctly closed in a later scriptlet for the page to successfully compile. This allows code to be intermingled and can result in poor programming practices. Content that falls inside a split block of Java code (spanning multiple scriptlets) is subject to that code. Content inside an
if block will only appear in the output when the
if condition evaluates to true. Likewise, content inside a loop construct may appear multiple times in the output, depending upon how many times the loop body runs.
Example The following would be a valid
for loop in a JSP page: Counting to three: This number is . OK. The output displayed in the user's web browser would be: Counting to three: This number is 1. This number is 2. This number is 3. OK.
Standard JSP Tags The useBean Tag The JSP useBean tag enables the developer to access and create a Javabean. Although using the useBean tag looks similar to an HTML tag, all JSP tags for JavaBeans use XML syntax. Therefore the code containing the useBean tag is case-sensitive. The useBean tag contains several attributes. The id attribute declares the name that is used for gaining access to the bean. The class attribute declares the package and class for the bean. The scope declares the object responsible for storing the bean. The value for the scope defines the duration for which the bean is available for the rest of the java application to use. The scope can be one of the following four values: • The scope implies that the bean is located in the implicitly defined object, and is only available for the current page. By default, all beans have a scope of . • The scope implies that the bean can be found in the object. This bean can be accessed by all other JSPs and servlets that have access to the current request object. • The scope implies that the bean can be found in the object. This bean can be accessed by all other JSPs and servlets that have access to the specified object. • The scope implies that the bean can be found in the object. This bean can be accessed by all other JSPs and servlets that have access to the specified object.
The getProperty and setProperty Tags After a bean has been created using the useBean tag, the getProperty and setProperty tags can be used for getting and setting the properties of the bean. The JSP getProperty is used to get the property of created bean. The JSP setProperty tag is used to set the properties for a bean. For the getProperty and setProperty tags, the name attribute is used to specify the bean's name. So the name attribute must match the id attribute provided by the useBean tag.
Expression Language Version 2.0 of the JSP specification added support for the Expression Language (EL), used to access data and functions in Java objects. In JSP 2.1, it was folded into the
Unified Expression Language, which is also used in
JavaServer Faces. The JSP Expression Language uses a compact syntax which enables the developer to get attributes and JavaBean properties from a given request object. When using EL, a dollar sign ("$") must be added at the beginning of the code. The dollar symbol is followed by an opening brace ("{"), as well as a closing brace ("}"). The code is then written between the opening and closing braces.
Example The following is an example of EL
syntax: The value of variable in the object javabean is ${javabean.variable}.
Additional tags The JSP syntax adds additional tags, called JSP actions, to invoke built-in functionality. One such library is the
JSTL.
Jakarta Standard Tag Library Jakarta Standard Tag Library (JSTL) supports common tasks that must be performed in JSPs. Examples includes iteration and conditionals (the equivalent of "for" and "if" statements in Java). Such JSP files commonly use the alternative .jspx file extension, which usually causes the application server to validate the XML syntax. Since the usual JSP syntax <% ... %> is not valid in XML, a developer must use alternative tags provided by JSP. For example, the common <%@ page .. %> directive may instead be written as a <jsp:directive.page .. /> tag, and tag libraries are imported using
XML namespaces, instead of the usual <%@ taglib .. %> tag. == Compiler ==