Title: FT2283 Web Development
1FT228/3 Web Development
JSP Directives and Scripting elements
2JSP techniques
JSP provides variety of techniques to enable
dynamic processing
In this topic
- Scripting elements (java)
- Action elements and Java Standard Tags Library
3Development JSP Pages
- Q Do you need to use Java to develop JSP pages?
- A You can (using scripting elements), but you
dont have to and there are good reasons not to.
Can use other techniques provided as part JSP
technology, in particular The Java Standard
Tag Library (JSLT) provides a set tags which
provide the equivalent functionalty of writing
java code in JSP pages.
4Development JSP Pages Template Text
Template text all text within the JSP page that
is NOT JSP code For web sites, usually template
text HTML but Template text can be anything
WML for mobile devices, javascript, HTML, PDF
etc HTML (or whatever) is just "passed through"
to the client by the servlet created to handle
the page. The HTML can be created by whatever
tools you already are using for building Web
pages. e.g. FrontPage
5Development JSP Pages Template Text example
ltHTMLgt
ltBODYgt
ltTABLE BORDER2gt
lt
for ( int i 0 i lt 10 i )
gt
ltTRgt
ltTDgtNumberlt/TDgt
ltTDgtlt i1 gtlt/TDgt
lt/TRgt
lt
gt
lt/TABLEgt
lt/BODYgt
lt/HTMLgt
6Development JSP Pages Comments
A comment in a JSP page looks like lt--
calculate the sum of x and z here
--gt Everything between the lt-- and --gt is
ignored by the JSP container The comments are
JSP code elements so are NOT send to the browser
(so not visible in View source) -? Hidden
comments
7JSP Pages Directive Elements
- Directive Elements - Used to provide information
about the general set-up of the page. e.g.
inclusion of header files, name of page to report
errors, whether session tracking is required etc - Always enclosed between lt_at_ gt
- Syntax lt_at_ directivename attribute
value, attribute value . gt
8JSP Pages Directive Elements
- There are Three directives available to use
- lt_at_ page .. gt
- lt_at_ include gt
- lt_at_ taglib .. gt
9Directive Elements
- Each directive has a set of associated attributes
(similar to some tags in HTML) - Usually placed at top of JSP file but doesnt
have to be - Example
- lt_at_ page import"java.util., java.lang." gt
- Full list of attributes available at
http//java.sun.com/products/jsp/syntax/1.1/syntax
ref11.html
10Directive Elements Page
Page directive - defines attributes that apply to
an entire JSP page. Examples lt_at_page
contentType text/html gt lt_at_page language
java gt lt_at_ page errorPage"error.jsp" gt
11Directive Elements Page
List of attributes includes lt_at_ page
language"java" import"package.class
package., ..." session"truefalse"
isThreadSafe"truefalse" multiple threads
allowed or not info"text" gives info about
the page to administration errorPage"relativeUR
L" contentType"mimeType charsetcharacterS
et " "text/html charsetISO-8859-1"
isErrorPage"truefalse" Specifies whether
exception object available or notgt
12Directive Elements Page
- Details for all Page directive attribute can be
found at http//java.sun.com/products/jsp/syntax/1
.1/syntaxref11.html - Home page for this is located in Web
development on distrib/ft228-3 - E.g. extract for page directive
13Directive Elements Include
Include directive - Includes a static file in a
JSP file attranslation time Syntax lt_at_ include
file"relativeURL" gt The included file can be
an HTML file, a JSP file, a text file, or a code
file written in the Java programming
language Useful for including repetitive HTML
content across a web site headers, navigation
bars, footers etc Useful for common logic e.g.
date displays
14Directive Elements Include
Example jsp page name includexample.jsp
lthtmlgtltheadgtlttitlegtAn Include Testlt/titlegtlt/headgt
ltbody bgcolor"white"gtltfont color"blue"gtThe
current date and time islt_at_ include
file"date.jsp" gtlt/fontgtlt/bodygtlt/htmlgt
15Directive Elements Include
Example (continued) jsp page name date.jsp
lt_at_ page import"java.util." gtlt (new
java.util.Date() ).toLocaleString() gt
Included into includexample.jsp from previous page
When includexample.jsp is run, displays as The
current date and time areAug 30, 1999 23840
16Directive Elements Taglib
Taglib directive - Defines a tag library and
prefix for the custom tags used in the JSP page.
Syntax lt_at_ taglib uri"URIToTagLibrary
prefix"tagPrefix" gt Example lt_at_ taglib
urihttp//java.sun.com/jstl/core prefixc"
gt More on taglib directives later when we use
Java Standard Tag Library (JSTL)
17Directive Elements summary
- Three directives page, include, taglib
- Used to define general set-up information about
the JSP page - By themselves, dont do anything
- At least one used in most JSP pages
18JSP dynamic processing
Done
19Scripting elements
Developers in JSP can insert java code directly
into a JSP pages using scripting elements The
code is executed when the page is requested
- Should be used with extreme care
- Too much code ? difficult to maintain
- Difficult for HTML programmers
- More suitable for simple applications with small
development team
Q How do you specify that the language being
used in page by scripting elements is java?
A Page directive language attribute
20Scripting elements
- Three types of scripting elements
- Expressions The expression syntax lt ... gt
defines a scripting language expression ..
result - Scriptlets The scriptlet syntax lt ... gt can
handle declarations, expressions, or any other
type of code fragment valid in the page scripting
language. - When you write a scriptlet, end the scriptlet
with gt before you switch to HTML, text, or
another JSP tag - 3. Declarations The declaration syntax lt! ...
gt declares variables or methods.
21Scripting elements Expressions
- Expressions
- Contains any valid java expression in the JSP
page - Used to output dynamic values directly onto web
page (e.g. result of a calculation, dates) - Enclosed between lt and gt
- output as a string
- Syntax lt expression gt e.g. lt 11gt
22Expressions - examples
- E.gs any valid java expression
- lt Math.sqrt(2) gtlt itemsi gtlt a b
c gtlt new java.util.Date() gt
23Scripting elements Expressions
Example lthtmlgt ltbodygt Current time is lt
new java.util.Date() gt lt/bodygt lt/htmlgt
24Scripting elements Expressions
- Note
- Evaluated at run time. Result is added to the
response body and output directly to web page - Can use an expression within a line of text,
whether or not it is tagged with HTML - Must be a valid java expression
- No required at end of expression (unlike
scriptlets)
25Scripting elements
- Three types of scripting elements
- Expressions
- Scriptlets
- Declarations
26Scripting elements Scriptlets
- Scriplets are java code fragments with a JSP page
- Enables more complex functionality than
expressions - Java code is placed between lt and gt characters
(just like expressions, but without the sign at
the start of the sequence.) - Can have any number of valid java code statements
- Scriptlet contains Java code that is executed
every time the JSP is invoked - Syntax lt code gt
27Scripting elements Scriptlets
Example jsp page that outputs numbers 1 to 10
onto a web page
htmlgt ltheadgt lttitlegtCount to 10 in JSP
scriptletlt/titlegt lt/headgt ltbodygt lt for(int
i1ilt10i) gt ltigtltbr/gt lt gt
lt/bodygt lt/htmlgt
Scriptlets
Expression used to output to page. Cant put
HTML Tags into the scriptlet. Can onlycontain
valid java code
28Scripting elements Scriptlets
Example jsp page that outputs numbers 1 to 10
onto a web page Output in browser will be..
Count to 10 in JSP 1 2 3 4 5 6 7 8 9 10
29Scriplets mixing scriplets with HTML and other
tags
- When you mingle scripting elements with HTML and
JSP tags, you must always end a scripting element
before you start using tags and then reopen the
scripting element afterwards, like this - lt else gt lt!-- closing the scriptlet
before the tags start --gt... tags follow
...lt gt lt!-- reopening the scriptlet to
close the language block --gt - At first, this may look a bit strange, but it
ensures that the scripting elements are
transformed correctly when the JSP source file is
compiled
30Scripting elements Scriptlets
Example 2 jsp page that outputs Hello! The
time is now Wed Sep 03 131758 BST 2003 Your
machine's address is 127.0.0.1 onto a web page
ltHTMLgt ltBODYgt lt java.util.Date date new
java.util.Date() gt Hello! The time is now lt
out.println( date ) out.println( "ltBRgtYour
machine's address is " ) out.println(
request.getRemoteHost()) gt lt/BODYgt lt/HTMLgt
Note could have put this line outside the
scriptlet as HTML
31Scripting elements Scriptlets
Note Any text, HTML tags, or JSP elements you
write must be outside the scriptlet Readability
Mixture of Tags and Java code can be
difficult to read especially for HTML
developers
32Scripting elements Scriptlets
lt if (Math.random() lt 0.5) gt Have a
ltBgtnicelt/Bgt day! lt else gt Have a
ltBgtlousylt/Bgt day! lt gt
In background, JSP container compiles the JSP
page into Java code.. converting the HTML
snippets into java code to out.print statements
---?
33Scripting elements Scriptlets
JSP code on previous page will be converted in
background to the following java code..
if (Math.random() lt 0.5)
out.println("Have a ltBgtnicelt/Bgt day!")
else out.println("Have a ltBgtlousylt/Bgt
day!")
34Scripting elements Scriptlets
Example 3 fragment of JSP page that generates a
table in HTML containing numbers 1 to n
ltTABLE BORDER2gt lt for ( int i 0 i lt n
i ) gt ltTRgt
ltTDgtNumberlt/TDgt ltTDgtlt i1 gtlt/TDgt
lt/TRgt lt gt lt/TABLEgt
HTML within for loop is output n times
Note would need to supply int variable n before
it will work
35Scripting elements
- Three types of scripting elements
- Expressions
- Scriptlets
- Declarations
36Scripting elements Declarations
Declaration Declares a variable or method that
can be used throughout the JSP page Examples lt
! int i 0 gtlt! int a, b, c gtlt! Circle a
new Circle(2.0) gt Syntax lt!
declarations gt
Note the
37Scripting elements Declarations
Declarations dont generate any output onto the
page by themselves -? usually used with
expressions and/or scriptlets Examples lt!
private int accessCount gt Accesses to page
since server reboot lt accessCount gt
Prints the number of times the current page has
been requested since the server was booted
38Scripting elements Declarations
Example Declares method getDate()
lt_at_ page import"java.util." gt ltHTMLgt ltBODYgt lt!
Date theDate new Date() Date
getDate() System.out.println( "In
getDate() method" ) return theDate
gt Hello! The time is now lt getDate()
gt lt/BODYgt lt/HTMLgt
Method getDate() returns a Date object
39Declaration
- Declarations (between lt! and gt tags) contain
one or more variable or method declarations that
end or are separated by semicolons - lt! int i 0 gtlt! int a, b double c gtlt!
Circle a new Circle(2.0) gt You must declare
a variable or method in a JSP page before you use
it in the page. The scope of a declaration is
usually a JSP file, but if the JSP file includes
other files with the include directive, the scope
expands to cover the included files as well.
40Declarations
- Warnings!
- Declaring variables in a JSP page using
declarations can cause synchronisation problems..
Compiled servlets see these as variables which
may be shared across all users using the JSP
page. - On previous date example, Date stays the same
when the page is reloaded.. Because only a single
instance of the page (and there of the variable
theDate is available). Better to use local
variables within declared methods or scriptlets
41Scripting elements - Summary
- Scripting elements enable java code to be
embedded directly into the JSP page - Be aware of code readability, complexity,
maintainability - Three forms of Scripting elements
- Expressions lt .. gt
- Scriptlets lt ..gt
- Declarations lt! . gt
42Topic summary
Page Include Taglib lt_at_ gt
- Directive elements
- Specify set-up info
- about the JSP page
Expressions lt gt Scriptlets lt code
gt Declarations lt! gt
- Scripting elements
- Enable java code to be embedded into the JSPpage