Download - Introduction to JSTL
-
7/27/2019 Introduction to JSTL
1/64
Introduction to JSTL
(JSP Standard Tag Library)
-
7/27/2019 Introduction to JSTL
2/64
As J2EE programmers, we are familiar with Servlets , JSPand JavaBeans. Any JSP page should encapsulate thebusiness logic in a bean and invoke it by using tag.
Till recently, a combination of Servlets, JSP and beans wasthe standard practice. But, the JCP released an API forenabling programmers to create custom tags and use themin their JSP pages.
The difference between javabean and java custom tags wasthat, though both made use of java classes, tags can beused by non-programmers also without knowledge of Javaprogramming, just as they would use html tags.( From aprogrammer's perspective,however, a much moreimportant distinction is that tags are specific to the page inwhich they are created while javabeans are general. )
-
7/27/2019 Introduction to JSTL
3/64
Java, JSP, and JSTL
-
7/27/2019 Introduction to JSTL
4/64
// greeting.htm=============
-
7/27/2019 Introduction to JSTL
5/64
(relevant section of greeting.java servlet)// greeting.java ( code-snippet only)public void doPost(HttpServletRequest req,
HttpServletResponse resp)throws ServletException,IOException {
resp.setContentType("text/html");PrintWriter out = resp.getWriter();//-------------------------------String s = req.getParameter("text1");out.println("");
out.println("we welcome"+",
");out.println (s);out.println(" ");}
-
7/27/2019 Introduction to JSTL
6/64
// greeting1.jsp
-
7/27/2019 Introduction to JSTL
7/64
----------------------------------------------// greeting2.jsp
we welcome
-
7/27/2019 Introduction to JSTL
8/64
// greeter.java//==============
package ourbeans;
public class greeter {
public greeter() { }public String greetme(String s) {
return "we welcome..."+s;}}
-
7/27/2019 Introduction to JSTL
9/64
/ greeting3.jsp
-
7/27/2019 Introduction to JSTL
10/64
// greeting4.jsp ( uses JSTL)
=========== We welcome
-
7/27/2019 Introduction to JSTL
11/64
There are five groups under which theJSTL tags have been organized.
They are as follows:
1) core
2) xml
3) sql
4) formatting 5) functions.
-
7/27/2019 Introduction to JSTL
12/64
JSTL and other frameworks
JSTL works with JSP versions 1.2 andhigher.
JSTL inherits all the benefits from JSP
JSTL will be useful to you whether you useStruts, JavaServer Faces, a differentframework, or nothing.
-
7/27/2019 Introduction to JSTL
13/64
JSTLA Template System
Web browsers dont care how web pages areproduced.
To a web browser, it makes no differencewhether the page its displaying is static(unchanging) or dynamic (produced by aprogramming language or template system)
Template systems like JSP and JSTL are similar toa word-processing feature called mail merge.
In a web template system, template text is mixed with anumber of placeholders.
These placeholders are filled in every time the pageneeds to respond to a web request.
-
7/27/2019 Introduction to JSTL
14/64
JSP Tags
An interesting subtlety about JSP tags and HTMLtags is worth highlighting:
You can use JSP tags inside HTML tags, because theseHTML tags are just arbitrary template text.
For instance, you can write:
If s purpose is to print a URL, then this tag mightbe replaced with and it works fine
However, JSP tags cannot appear inside another JSP
tags attributes. For instance, if and are both JSP
tags, then you cant write
-
7/27/2019 Introduction to JSTL
15/64
Standard JSP Tags
Some JSP tags are built into JSP; theyreeffectively hard-wired into the JSP standard.
These tags are often called standard tags,although the term is somewhat confusing.
This group of standard tags doesnt includeJSTL tags; instead, it includes core JSP tags thatpredate JSTL by several years.
JSTLs tags are also standard, but they fall intoa separate group of tags
-
7/27/2019 Introduction to JSTL
16/64
Standard JSP Tags
-
7/27/2019 Introduction to JSTL
17/64
Standard JSP Tags
Lets you include one JSP page from withinanother, including the static content
Always looks for files on the local server Ignores the htmls tag
Other related tags
-
7/27/2019 Introduction to JSTL
18/64
JSP Directives
Directives are pseudo-tags that havespecial meaning to the container;
they are not passed through to the browser
but, instead, are processed by the JSP engine directive
directive
directive
-
7/27/2019 Introduction to JSTL
19/64
directive
e.g.,
Different from
directive works by finding the
target file and inserting it into your JSP page, justas if you had cut and pasted it using a texteditor.
By contrast, locates the targetpage while your JSP page is executing.
When page A uses to includepage B, page Bs data is simply included in pageA every time it is compiled
-
7/27/2019 Introduction to JSTL
20/64
vs. If a file included with changes, its
changes will not be noticed until the page containing thedirective also changes page A must be changedand recompiled for any changes
in B to take effect notices changes immediately
cannot work with Servlets is more efficient than , but
it also uses much more disk space when large files areincluded.
With , the two pages involved are twoentirely separate pages They can use the same names for different variables, or they
can use different prefixes for the same tag library With , because page A and page B are
essentially merged before being compiled, there might beclashes between names within the two pages
-
7/27/2019 Introduction to JSTL
21/64
directive
, lets you modify someproperties of a JSP page
This directives goal is to provide meta-
information about how to process thepage
-
7/27/2019 Introduction to JSTL
22/64
-
7/27/2019 Introduction to JSTL
23/64
The directive
Tag libraries use prefixes other than jsp, andthey must be explicitly imported into pagesbefore they can be used.
Thus, whereas the jsp: tags can be used in anyJSP page without fanfare or preparation, youneed to introduce others (including JSTLs) usinga special pseudo-tag known as a directive.
Think of JSP directives as being somewhat like
the HTML tag their function is not specifically to display anything in
the browser, but instead to describe some informationabout the page itself.
-
7/27/2019 Introduction to JSTL
24/64
The directive
Directives are one JSP feature that doesntstrictly follow an XML-like syntax.
Instead, a directive begins with .
One such directive, , is used toimport a tag library into a page.
Even though they begin with , directives are similar to XML tags in thatthey accept attributes.
The directive requires twoattributes: uri and prefix.
-
7/27/2019 Introduction to JSTL
25/64
-
7/27/2019 Introduction to JSTL
26/64
Tag Libraries - URI and Prefix
Knowing a librarys URI or file path, you can usethe directive to register it and,at the same time, assign it an XML-likenamespace prefix for use within the page.
Imports the tag library identified by the URIhttp://www.acme.com/custom.tld into the page, usingthe prefix acme.
After this directive appears in a page, tags from thelibrary can be used with the acme prefix.
-
7/27/2019 Introduction to JSTL
27/64
-
7/27/2019 Introduction to JSTL
28/64
Core Library
The core library includes tags for thefollowing uses:
Accessing and modifying data in memory
Making decisions in your pages Looping over data
Suggested Prefix: c
URI: http://java.sun.com/jstl/core
Example:
-
7/27/2019 Introduction to JSTL
29/64
XML processing library
The XML library includes tags for thefollowing purposes:
Parsing (that is, reading) XML documents
Printing parts of XML documents Making decisions in your page based on the
contents of an XML document
Suggested Prefix: x
URI: http://java.sun.com/jstl/jstl/xml
Example:
-
7/27/2019 Introduction to JSTL
30/64
Internationalization (i18n) & formatting
The formatting and internationalizationlibrary includes tags for these uses:
Reading and printing numbers
Reading and printing dates (with support fortime zones)
Helping your application work with more thanone language
Suggested Prefix: fmt URI: http://java.sun.com/jstl/jstl/fmt
Example:
-
7/27/2019 Introduction to JSTL
31/64
Database (SQL) access
The SQL library helps you read and writedata from databases.
Suggested Prefix: sql
URI: http://java.sun.com/jstl/jstl/sql Example:
-
7/27/2019 Introduction to JSTL
32/64
Using JSTL in your pages Before you can use a tag library, you need to import it.
For each page, you only need to import the libraries youactually use
Core
XML
Formatting
Database
-
7/27/2019 Introduction to JSTL
33/64
JSTL Tag Libraries
JSTL uses a simple language called anexpression language to make it easy foryou to access information
Before JSTL, you really had to know Javato produce an effective JSP page
JSTL makes writing pages easier.
Its expression language is much simplerthan Java; in fact, its even simpler thanJavaScript.
-
7/27/2019 Introduction to JSTL
34/64
Expressions and the tag
The most fundamental tag
used more often than any other tag in JSTL
Prints the result of an expression
tag is a little like JSPs and ASPs
prints the text, Hi there!
The tag becomes useful only when thevalue attribute contains an expression in JSTLsexpression language
-
7/27/2019 Introduction to JSTL
35/64
-
7/27/2019 Introduction to JSTL
36/64
default attribute
Nobody
"/>
-
7/27/2019 Introduction to JSTL
37/64
Accessing Data using JSTL
The major goal of the JSTL expressionlanguage is to make data easy to access.
This data can fall into a number of
categories scoped variables,
request parameters
-
7/27/2019 Introduction to JSTL
38/64
-
7/27/2019 Introduction to JSTL
39/64
Basic syntax to access scoped variables
${pageScope.username}
${requestScope.username}
${sessionScope.username}
${applicationScope.username}
JSTL tags also let you create and storescoped variables
-
7/27/2019 Introduction to JSTL
40/64
Basic syntax to access scoped variables
${sessionScope.shoppingCart} Such a variable refers to an entire collection of
objects, organized under a single name:shoppingCart.
${sessionScope.shoppingCart[0]} ${sessionScope.user.name}
${sessionScope.user["name"]} isequivalent to ${sessionScope.user.name}
R d h i
-
7/27/2019 Introduction to JSTL
41/64
Request parameters and the expression
language
One language you can read is
-
7/27/2019 Introduction to JSTL
42/64
Accessing other data with JSTL
Cookies
If youre told that a cookie called colorPreference isavailable, you can access it with an expression like${cookie.colorPreference}
Headers Web browsers send information about their make and
model to servers using a header called User-Agent
${header["User-Agent"]}
Initialization parameters
${initParam.headerUrl}.
-
7/27/2019 Introduction to JSTL
43/64
Comparisons You can use expression language to compare values
the expression ${2 == 2} results in true
Every comparison operator has a symbolic version (==)and a textual one (eq)
-
7/27/2019 Introduction to JSTL
44/64
-
7/27/2019 Introduction to JSTL
45/64
Saving data with Many JSTL tags let you create scoped variables;
the most basic is value
The expression to compute Required - No Default - Use body
var The name of the scoped variable to save Required - Yes Default - None
scope The scope of the variable to save Required - No Default - page
-
7/27/2019 Introduction to JSTL
46/64
This tag creates a page-scoped variable named
eight and sets it to the string 8, which is theresult of the tag
-
7/27/2019 Introduction to JSTL
47/64
Deleting data with
var
The name of the scoped variable to delete
Required - Yes Default - None
scope
The scope of the variable to delete
Required - No
Default - Any
-
7/27/2019 Introduction to JSTL
48/64
JSTL Flow Control
JSTLs flow control comes in two forms:
Conditional logic, or conditions
Looping, or iteration
-
7/27/2019 Introduction to JSTL
49/64
JSTL Decisions
Yes-or-no conditions with
Mutually exclusive conditions with, , and
-
7/27/2019 Introduction to JSTL
50/64
Yes-or-no conditions with
test Condition to evaluate. If true, process the body; if false,
ignore the body
Required - Yes
Default - None
var Name of the attribute to expose a boolean value
Required - No
Default - None
scope Scope of the attribute to expose a boolean value
Required - No
Default - page
-
7/27/2019 Introduction to JSTL
51/64
test attribute The test attribute specifies a conditional
expression to evaluate.
--do something
if the test expression evaluates to false, the pageskips the body of the tag.
The body can contain any valid JSP code,including text, HTML tags, or other JSP tags
Dr.
-
7/27/2019 Introduction to JSTL
52/64
-
7/27/2019 Introduction to JSTL
53/64
The var and scope attributes -- Saves variableA serious error has occurred.
[ large page body]
-- Uses variableSince a serious error occurred, your data has not been saved.
-
7/27/2019 Introduction to JSTL
54/64
-
7/27/2019 Introduction to JSTL
55/64
Mutually exclusive conditions
Error 1 has occurred Error 2 has occurred Error 3 has occurred
Only one of these tags can succeed
Error 1 has occurred Error 2 has occurred Error 3 has occurred Everything is fine
Addition of to display the default message
-
7/27/2019 Introduction to JSTL
56/64
JSTL Looping
General-purpose looping with
Iterating over strings with
General purpose looping with
-
7/27/2019 Introduction to JSTL
57/64
General-purpose looping with
The basic function of is to considerevery item in the collection specified by its itemsattribute.
For each item in the collection, the body of the
tag will be processed once, the current item is exposed as a page-scoped variable
whose name is specified by s varattribute.
Iterating over strings with
-
7/27/2019 Introduction to JSTL
58/64
Iterating over strings with
A token is a single, discrete unit within alarger string.
The tag iterates over
tokens, which it parses from an inputstring
Iterating over strings with
-
7/27/2019 Introduction to JSTL
59/64
Iterating over strings with
-
7/27/2019 Introduction to JSTL
60/64
Looping over numbers
Output: 1 2 3 4 5
Output: 2 4 6 8 10
-
7/27/2019 Introduction to JSTL
61/64
Accessing Database with JSTL
The tag does not support connectionpooling.
SELECT NAME, IQ FROM USERS WHERE IQ > 120
-
7/27/2019 Introduction to JSTL
62/64
Updating Database with JSTL
INSERT INTO PEOPLE(NAME, AGE, WEIGHT) VALUES(John"Fatso" Smith, 34, 540)
DELETE FROM CUSTOMERS WHERE AGE < 18
Our CUSTOMERS table had minors.They have all been removed. Close call; were lucky the Feds
didnt come after us.
-
7/27/2019 Introduction to JSTL
63/64
SELECT * FROM USERS WHERE BIRTHDAY < ?
-
7/27/2019 Introduction to JSTL
64/64
JSTL in Action
With this, you have covered approx. 8chapters of the book JSTL in Action(Manning)
For additional insight please refer the book Read Chapter 10 of the book regarding
formatting & internationalization itsinteresting