Title: Web Development
1Scripting elements
- Three types of scripting elements
- Expressions
- Scriptlets
- Declarations
2Scripting 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
3Scripting 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
4Scripting 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
5Scriplets 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
6Scripting elements Scriptlets
Example 2 jsp page that outputs Hello! The
time is now Wed Sep 03 131758 BST 2006 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
7Scripting 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
8Scripting 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
---?
9Scripting 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!")
10Scripting 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
11Scripting elements
- Three types of scripting elements
- Expressions
- Scriptlets
- Declarations
12Scripting 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
13Scripting 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
14Scripting 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
15Declaration
- 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.
16Declarations
- 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
17Scripting 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
18Topic 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