You are on page 1of 35

JSP Standard Tag Libraries (JSTL) : Java

Module Objective
At the end of this module, participants will be able to understand:
What EL is How to use Arithmetic operations in EL How to disable/enable evaluation of EL for a JSP How to disable/enable evaluation of all the JSPs in application How to use Comparison operations in EL What are implicit objects in EL and their usage What JSTL is JSTL tags JSTL requirements How to use out tag of core library How to use set and if tags of core library How to use choose, forEach, forTokens, redirect and param tags of core library
2

Introduction to EL
EL stands for Expression Language. EL was introduced in JSTL1.0. Before EL was introduced, scriptlets were used to manipulate the application data. EL makes it possible to access the data stored in Java Bean components. The server side state is integrated with the presentation output using EL. EL expressions can be used in static text. An EL expression is written between the delimiters ${ and } The expression and its delimiters are replaced by the result of expression evaluation.

EL - Arithmetic Operations

EL - Arithmetic Operations - Output


The output of the previous JSP page is as under:

Deactivating evaluation of EL
To deactivate evaluation of EL, the isELIgnored attribute of page directive is specified:

The valid values of isELIgnored attribute are true and false. If the value of isELIgnored attribute is true, EL expressions are ignored and they appear in static text. If the value of isELIgnored attribute is false, EL expressions are evaluated by the container. The default value of isELIgnored attribute is false.

Deactivating EL in all JSPs


In order to disable EL in all JSPs in a web application, insert the following <jsp-property-group> to the applications deployment descriptor file (web.xml):

EL Comparison Operations

EL Comparison Operations - Output


The output of the previous JSP page is as under:

Implicit Objects
Implicit objects defined by JSP Expression Language are:
pageContext = It refers to the context for the JSP page. It contains the reference of the objects like request, response, session and servletContext.
param = Using this, a request parameter name is mapped to a single value. paramValues = Using this, a request parameter name is mapped to an array of values. header = It maps a request header. cookie = It maps a cookie name to a single cookie. initParam = It maps the context initialization parameter name.

Using param implicit object


Using param implicit object, a request parameter name is mapped to a single value. Consider the following 2 JSP pages:
paramEx.jsp

paramOutput.jsp

Using param implicit object - Output

Using initParam implicit object


initParam implicit object maps a context initialization parameter name.
Specify <context-param/> tag in web.xml

initParamOutput.jsp

Using initParam implicit object - Output

Using cookie implicit object


cookie implicit object maps a cookie name to a single cookie.
CookieServlet.java

cookieOutput.jsp

Using cookie implicit object - output

What is JSTL?
JSTL stands for JSP Standard Tag Libraries. JSTL is a collection of JSP custom tags which have been developed by the Jakarta project. JSTL can help to avoid scripting elements in JSP pages. A JSP page should not have scriptlets containing the Java code. It should be a view page. JSTL offers custom tags through 4 libraries:
core: Provides scripting functions xml: Used for xml processing fmt: Used for internationalization sql: Used for database processing

JSTL tags
Area
Core

Subfunction
Variable support Flow Control URL Management Miscellaneous Core Flow Control Transformation Locale Message Formatting Number and Date formatting SQL Collection length String manipulation

Prefix
c

XML

I18N

fmt

Database Functions

sql fn

JSTL Requirements
The requirements to use JSTL are: Requirement A JSP Server A JSTL implementation Description Apache Tomcat from apache.org Taglib from apache.org jstl.jar and standard.jar

A JSTL document
JSTL pages having JSTL tags

JSTL specification
To be written by you

Hello World Example with JSTL


Lets try a very simple example to understand how JSTL works. Consider the following code:

Run the above jsp page in browser. The following output is displayed:

Core Tag Library


The various tags present in the core tag library are as follows:
set out if choose when otherwise forEach forTokens import url redirect param

Using out tag of core library


Consider the following JSP page:
displayData.jsp

Using out tag of core library - Output

Using set and if tags of core library


Consider the following JSP page:
ifExample.jsp

Using set and if tags of core library - Output

Using choose tag of core library


Consider the following JSP page:
chooseExample.jsp

Using choose tag of core library - Output

Using forEach tag of core library


forEachExample.jsp

Using forEach tag of core library - Output

forTokensExample.jsp

Using forTokens tag of core library

Using forTokens tag of core library - Output

Using redirect and param tags of core library


redirectExample.jsp

welcome.jsp

The redirect tag is used for forwarding the browser to the specified url. The param tag is used to send some parameter value to the page to which the redirect occurs

Using redirect and param tags of core library Output

Key Points
After completing this course you should be able to:
Tell what EL is Demonstrate how to use arithmetic operations in EL Demonstrate how to enable/disable evaluation of EL Tell what are the various implicit objects in EL Tell what is JSTL and what are various JSTL tags Demonstrate how to use various tags of core library

Questions and Comments

35

You might also like