Mark Cyzyk. Scr ... s, v5 (8), August 2000 - JScholarship

fawnmoldwarpInternet and Web Development

Nov 10, 2013 (3 years and 9 months ago)

136 views

ctll;llll
||]lrEc{lcrtill
fiar53eftl
\rllllltl
rlrrlltl
#3333!
ffi3333!
--zZGllOll
ffi3333t
F___rtOOaa
frrullll
)loollltl
trrrltttt
/rl.lttlll
llrlllllr
lsrlllllr
ro6altllr
r$mllrp$lf
lfllTillrH*tstl
rlldllrfl
-.rll-r--l
I
I
t
s
sls
3
I
ll
ll
lr
lr
lr
lr
ll
ll
lt
ll
ll
ll
n
)l
i
ililil]|
t -
tl
'lll
orl
3t
?f'
jil,*
,
'-
lffi
lffi
a.r
orl
lll
llr
llr
llr
lll
TIG
)lc
llff
Jlr
lll
illl
illl
llt
lll
lll
tll
lrl
ll|.
lll
lll
lll
lll
lrr
lll
lll
ttl
llt
rl*
-,,-,.-.
llllllililililllufl|il
- F - -
tflfl
SCRIPT
JUNKIE
The
Yale comPuter
scientlst David
Gelernter,
in hls book
Mochine
Beouty:
Elegonce and
the Heort of Technology'
persuasi vel y argues that much
of the devel opment
of supe-
ri or technol ogy
rel i es not so much
on what
the technol ogy
does, but how
i t does what i t
does. Speci fi cal l y,
Gel ernter
asks,
does the
technol ogy work
i n an el egant
manner and
i s
i t
si mpl e,
Yet Powerful?
Consi der the
sel ecti on of
server-si de scri pti ng
l anguages: The
bi g contenders
i n thi s arena
are Acti ve
Server Pages
(nse) and
Javaserver
Pages
(JSP)' Hypertext
Preprocessor
(PHP) i s al so starti ng
to edge
i n on the
market. Whi l e
each i s
powerful i n i ts own
way, nei ther of
the envi ronments
i s
parti cul arl y aestheti c
by Gel ernter's
defi ni ti on. Performi ng
si mpl e
tasks wi th
these technol ogi es
j
often requi res
a l ot of overhead,
resul ti ng i n
code-heavl'
I
convol uted
programs'
I
tf yor't. choosi ng
a server-si de
scri pti ng
l anguage'
you
]
shoul d consi der
a fourth opti on:
Col dFusi on'
Because i t
acts
!
more l i ke a markup
l anguage
than a
programmi ng
I
l.ngr.g., Col dFusi on
i s an
el egant sol uti on
for embeddi ng
I code i n HTML
fi l es. Al l ai re,
the company that
al so makes
1
the popul ar Homesi te
HTML edi tor,
has a whol e
sui te of
'
Col dFusi on technol ogi es
consi sti ng
of an appl i cati on
server'
, an i ntegrated
devel opment envi ronment,
and
of course' the
,
markup l anguage'
l t i s thi s Col dFusi on
Markup
Language
(cFML) that
competes di rectl y
wi th ASP and JSP'
pohtel'
Wi thi n cFML
you'l l fi nd al l the
usual condi ti onal
l ogi c
constructs,
data
structures, and
uti l i ty functi ons
avai l abl e
i n
most mature
programmi ng l anguages'
Further'
CFML
provi des tags to
easi l y i mpl ement
emai l i nteracti ons'
LDAP
i ntegrati on, and
FTP and HTTP agent
creati on-functi ons
i
that other Web
appl i cati on devel opment
pl atforms requi re
!
thi rd-party modul es
to support'
Fi nal l y, the Web
appl i cati on
, - L^ - ^ L., ^ - +^ ^,1-
I deuel oper can
create custom
tags i n cFML, thereby
extend-
I
i ng tne l anguage's
Power.
.'
i.
':l
:S
onl j ne
r es0 ur ces
Need more
information
about
ColdFusion and
some
of
the processes
described in this
article?
You'll find
it
here.
elegant
*
I
*
E
4
.t
]
Companative
Languages
For a better
exampl e of cFML's
si mpl i ci ty,
power, and
el egance, consi der
queryi ng a database'
Li sti ng t shows
code that runs
a SqL statement
agai nst
a l ocal data source'
retri eves a
recordset, and
di spl ays the resul ti ng
data' Li ter-
al l y, that's
al l you have to do-connect
to
the source' seno
the SQL command,
and di spl ay
the resul ts'
Not bad for
ni ne
l i nes of scri Pt!
Now take
a l ook at what
you woul d have
to do wi th
AsP
and
JSP.
Li sti ng
z shows
an AsP scri pt that
does the same
task, and
Li sti ng
3
shows
the equi val ent JSP
scri pt' In both
cases, noti ce
the wordy and
convol uted syntax
and compare
i t to the CFML
scri Pt i n Li sti ng
t.
A Web
Agent
O-ueryi ng a
data source i s
one l i mi ted exampl e'
CFML
al so works
wel l for a number
of other tasks'
Li sti ng
4
reDresents
a smal l Col dFusi on
appl i cati on
that serves
as
a Web agent.
l t takes a Web
page that
you speci fy and
sends i t through
the Al tavi sta
gabel fi sh
l anguage
transl ati on
servi ce. The resul t
i s the same
Web page'
transl ated i nto
the l anguage
of your choi ce'
Furthermore'
the scri pt
al ters each hyperl i nk
i n the document
so that
sel ecti ng any
one al so returns
a transl ated
page' In thi s
way, once
you've transl ated
a page i nto Portugese'
al l
l i nked HTML
documents from
that
poi nt are automati cal l y
transl ated i nto
Portuguese.
To run thi s
code
you'l l
need
to have the
Col dFusi on
server software
i nstal l ed
and connected to
your Web server'
The Col dFusi on
server acts as
the i nterpreter
for al l your
col dFusi on markup.
Thi s
process i s si mi l ar
to enabl i ng
your
server to handl e
ei ther ASP or
JSP.
The key thi ng
to note about
thi s code i s
i ts si mpl i ci ty'
Li ne t of the
code i n Li sti ng
4
si mpl y sets
a vari abl e usi ng
the
<cf
set>
tag. (Rl l tags
i n Col dFusi on begi n
wi th cf ')
Thi s vari abl e
hol ds the URL for
the page that
the scri pt
shoul d i ni ti al l y
retri eve; i n thi s
case i t's a
page on theJohns
Hopki ns
Uni versi tY
Web si te.
Li ne
3
dynami cal l y
creates
a base URL from
the hostname
of the current
server. You coul d
j ust
hard-code
the domai n
name i nto
the scri Pt, but
generati ng i t dynami cal l y
saves
you ti me i f
you l ater move the
appl i cati on to
another
server.
Li ne
7
i s the
key to the appl i cati on'
Here
the
<cfhttp>
tag
requests retri eval
of a
parti cul ar document
from
the Web. The Col dFusi on
server
recei ves the
document'
It's cruci al to
real i ze that the
page i sn't automati cal l y
returned to
the browser' Instead,
the col dFusi on
server
has the opportuni ty
to fi l ter
(or otherwi se
mani pul ate)
the page's contents.
Thi s becomes
i mPortant
l ater i n the
CFML
may not
of f er t he
r i ch f unct i onal i t y
of ot her l anguages,
but i t can
be
an el egant
choi ce
i f t he pr oi ect
i s r i ght'
74
www.webt echni ques.com
august 2000
<cfsueFu
datasouFce="sonedatasounce"
SELECT IASTIIIA'IE,
FIRSTNA}IE
FROIit tiIAIN
ORDER
BY LASTITIAIIE
</cfquerg>
<cf outDut
queng=t'sonequeFgnamrr>
#LASTNi\I{E*,
#FIRSTltlAllE{+<bn>
</cfoutDut>
obj RS.l l oveNext
lr'lend
obj RS. ctose
obi Conn.
cl ose
Se{ obiRS
=
lrlothlng
set obj conn
=
Nothi ng
%>
46
page l mpont="j ava'sql'*"
'(>
4
tFu
(
i i l ss. ronram
r
"sun. Jdbc'
odbc' Jdbc0dbcDni ver"
)
;
i'liiiit-
tii*.r"ng'ciassl{otrounoException
e}
(
e,pnJntstackTrace
(
t
;
l
Connection
rxJconneetlon
=
nuttl
Statement
rustatement
=
nu-L-L;
Resultset WResuttset
=
nuLL;
llBoln."tron
-
Dnlvrllanagen.
getconnectlon,
{'
jdbc: odbc:
jsp"
mistatemnt
=
lryconneetlon'cFeatestatemenEt'
; -.
;fi
i;ilits;i
=
mlstatermnt'
executeoueng
(
n
select f i
Fstname'
listnam
fron maln
ondei bg lastname");
wh
lle
(ngResuttset.next
(
I )
lut 'pnlnttn
tWnesuttset
'
getstrlng
(
"lastnane"
)+i
"'l
;
iii:
fti;ii;
i.iRa"uiiiii
'
iitstnini
('rlFstnane" )+'<br>" )
;
)
mrResuttset.cl ose
(l
;
nistatenpnt.close
(
)
;
ni 0onnectl on.
ctose
(
l
;
)-catch
(Sql Excepti on e)
{
e,Dnl nt st ackTFace( );
)
%>
nam=" somequeFgnane ">
I
rt
5
5
7
I
I
10
12
13
14
1 t
IO
L7
18
t c
4
21
zi
zl
zl
A
<a
d-ANGUAOE
=
VBScrlPt
,(>
4
oDtl on
Exptl ci t
Ri soonse.ExPl nes
=
0
ol n
obi conn, obj Rs'
stro
Dl n stnoonnecti on
14:
"'-
-;i;;l f
=-senven.Cneateobj ect
(
"ABO0B'Connectl on"
)
:ilc;il;;fi
*
--idata sounce-iomedatasouFce;
n
ob iconn.OPen
stnconnect'lon
ili-i'd"iii':'si""in.cneateoljectt
"ADooB'RecoFdsettr
)
li:.;!iE;iE3l"li3lniif I
"ffi '?iii:"1
'il[o-="illooo"i[E$
S'i^l'** "
obj Rs.oPen
stFo
b
4
m:hri:
mli!':ljfu
1 P;1115r,',''.;*,"
I
appl i cati on.
Noti ce
al so the
cl ean syntax
of
i
th.
..fhttpt tag. Just
pass i t a URL,
tel l i t
i to
ei ther 6ET a
document
or P0ST to
a
I do.ut.na,
and
deci de whether
to ful l y resol ve
I
any
embedded
URLs i n the
document' Li ke
I
.rny
ttgt i n CFML,
thi s tag
l ets you do
i
extremely
powerful
things,
yet it's
I
extraordi nari l Y
si mPl e
to use'
i
t-in.t
9
through 3o
place an HTML form
at
I the
top of whatever
page resides
at the
I
stanti ngURL
vari abl e from
Li ne 1' You
shoul d
I set
stantingURL
as the first
page
you want
I translated.
the form
lets users
select source
I and target
l anguages,
l i ke "Engl i sh
to German"'
i for exampl e.
When
the user
cl i cks on the
i "Transl.te"
button
a
JavaScri pt
functi on trans-
of the
current document.
The
script also
appends
two other
URL vari abl es
to the
end
of thi s
stri ng:
thi sURL, whi ch
contai ns
the
val ue
of the stant'i ngURL;
and tp, whi ch
is
a variable Babelfish
requires
to identify
the
transl ati on
l anguage.
After
the scriPt
outputs this
HTML form
to the
user's browser,
it then
outputs the
contents
of the
#CFHTTP. FILECONTENT#
vari abl e.
(Al l vari abl es
i n Col dFusi on
are
surrounded
by
pound si gns.)
thi s vari abl e
contains
all the
HTML code for
the Web
page
previously retrieved
by the
<cfhttP>
tag'
Thi s
occurs i n l i nes 3t
through 33
of the
scri Pt'
l n
l i ne
39,
you'l l fi nd another
<cfhttP> cal l
that essentially
Passes
the
starting
page URL
to the Babel fi sh
servi ce.
To recap:
At thi s
poi nt the user shoul d
see
a
Web page with
a language
selector
at the
top i n hi s or
her browser.
once the
user
chooses a l anguage
and
cl i cks on the
Transl ate
button, the
Javascri pt
functi on
sends
the
appropri ate
commands
to
the Babel fi sh
service
for
processing. The
Babelfish
engine
processes
the
Page,
transl ates
i t i nto
the
sel ected
l anguage,
and
then returns
i t to
the ColdFusion
server where
further
processi ng
can occur.
once the Babelfish
service
returns
the
transl ated
page, l i nes
4t
and
4z
prepare
fers
the cl i ent
to the Babel fi sh
servi ce, wi th
a
request
to transl ate
the document
and send
i t
to the
ColdFusion
server'
perhaps for further
processi ng.
JavaScni Pt
OutPut
Noti ce
al so that
Li nes 12 through
14 use
ColdFusion
to output Javascript'
That is'
the scri Pt
sets the
val ue of a
Javascri pt
vari abl e
wi th
the val ue
of a Col dFusi on
vari abl e'
In
thi s
case, the
Javascri pt
unl
vari abl e,
whi ch
wi l l
be the l ocati on
to whi ch
the cl i ent
navi gates,
contai ns
the ful l y
qual i fi ed URL
www.webt echni quEs.com
august 2000
L
2
,t
5
6
7
8
9
Ig
11
12
13
14
16
L7
I 6
19
2g
24
25
26
28
29
3g
31
32
33
34
35
JO
37
38
39
4g
41
+z
43
44
+5
46
I+7
<cf
set stantl
n
9URL
='http
: //if,rr,.
Jhu.
edulrdrl,/about jV,>
<cfset
futlgouallfledTerplate
=
"nttp.//"
&
#C6I.HTTP_H0ST#
& #C6I.pATH_ItrtFO#>
<!-----U
thi s
l s the fl pst
t.tme...
___>
<cfJf.
NoT
.tsDfl ned("URL.ttr.l sUnt;ti
<ctnttp
unt=n#stantJngURL#tr
nethod="gettr
Fesotveur.l_,,gesil>
;ff lli:".ililiiT"
javascFr
pt,'>
lp
=
do_cunent.
f orms
tot . elemnts
_tol
. optJ ons
tdocument.
f orms t
g
I
."routpui i l mnts
t0t . setectedl ndex
I . vai l i
j -
'-
-----"'-"
"
'
.,,"fllaiui3tuaar0uall
fl edTemprate#?thJ
suRl=#stanti
nguRl#&lpF' +
lp
;
)
document.Locati on.hnef
=
unl;
</scFlpt>
<dJv
atJgn=,,centeF'>
<form
name=nform1tr>
<font
si ze=u-l.
face="an.l al,
hel vetl ca,,><B>Tnansl ate
fnon:
<sel ect
nam="l p">
::itl:l
y:tl=::l:li;,:"itiJ;'isnsrlshnto French
<oprton
vatue=ren_l t">Engush
to
Ital i an
<opti0n
value=tren_pt">Enilish
to ioniuguese
:i3:1;:.y".r.=""n_es">Enirlsh
to spanlSh
<lnput
tgpe=nbutton'
onotlctc,'dolt (
)
"
valuF,,Tnanslate,,>Vb><,/font>
</d1v>
<cfoutDut>
#CFHTTP. FILECONTEI{I#
</cfoutput>
<cfetse>
-.i.--.
Il th1_s l s
the second
oF suDsequent
ti mes...
___>
:::;::,':l:L;dllii'#liftIi:q
",'ltF"t:iflrt';r-csr-bin/t'anstate?do1t=done&rn'1text='
_.gls"! lIl__E,cgryTEr,
=
*.rrrr,
]Fi _
set "Tnesotveunt='nb
">
::;::t,FiiE33frlF$l
:
[FiilSEllFi[3gllE[il:"r;fl*li1];"n;p;,=ii#ce[;frfx-rnro#", "ALL",>
#FILECOI{TENT#
<,/cfoutDut>
n>');
rage
URL
rul d
see
at
the
;er
Tra
nslate
the
sh
:ngine
the
t t o
</cflf>
the
page
to be sent
to the user.
Repl acement
functi ons
modi fy each
hyperl i nk
on the
page
so that
i nstead
of
poi nti ng
to
untranslated pages,
they refer to
the Babelfish
servi ce.
Note,
however,
that i n addi ti on
to these
si mpl e repl ace
functi ons,
you
can do much
more processi ng
on
the resul ts
of a
<cfhtto>
cal l. Col dFusi on
supports
a whol e
range
of regul ar
expressi ons
that coul d
radi cal l v
reformat
the content
of this page
before
returning
it to the
user's browser.
Such
regul ar expressi ons
coul d, for
exampl e,
extract specific
data
from a site,
like stock
and weather
i nformati on,
and
return-i t on
a newly formatted
page.
Noti ce
the power
and functi onal i ty
bui l t
i nto a
mere
47
l i nes
of scri pt. Thi nk
of other
programmi ng
l anguages
that you
know_coul d
you
perform
the same
task
wi th such economv
of expressi on?
Beauty
and Bnawn
It's true
that other
Web appl i cati on
l anguages
are far ri cher
than
CFML. There
are manv
programmi ng
tasks
that can
be accompl i shed.
for exampl e,
i n
Java
wi th i ts
Java
Foundati on
Cl asses
(the Swi ng
l i brary)
that si mpl y
can,t
be
accompl i shed
wi th CFML.
The Swi ng
l i brary
provi des
much more
Ul functi onal i tv
than
the HTML,
CSS, and
DHTML that
Col dFusi on
rel i es
upon for
i ts cl i ent_si de
user
i nterface. But
many appl i cati ons
don,t
requi re
the sophi sti cati on
these ri cher
functi ons provi de.
In short, you
shoul d
choose your l anguage
based
on the
task at hand.
l n many
cases, as
i n those
requi ri ng
standard
network protocol s
and
servi ces and
back-end
database
i nteracti ons,
the
extent of your
programmi ng
acti vi ti es
can
be best handl ed
wi th Col dFusi on
technol ogi es.
CFML hel ps you
achi eve your
programmi ng
goal s
effi ci entl y
and
rapi dl y,
and
accordi ng
to Davi d Cel ernter,
that pl aces
the
l anguage
i n the category
of superi or
technol ogi es.
><
Mark
is a Web
developer at
Johns
Hopkins
Univercity
and
Nine Web,
LLC.
you
can
contoct
him
ot mcyzyk@jhu.edu.
qd
er
nje.com
Unlimited
Unlimited
Unf imited
Unlimited
Domains
FTP
POP3
ODBC
100M8
Disk
Space
CGI
& ASP
Cold Fusion
Web
Controf Panef
Access
to
Raw
Logs
Clustered
Servers
Multiple
Connections
Clean
UPS Power
Only
US$25
per
month
Sha
Shanje.com
august 2000
www.uvebt echni ques.com
7j