JSP

thickbugΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 3 χρόνια και 11 μήνες)

69 εμφανίσεις

JavaServerPages
Java
 
Server
?
Pages
Contents
Contents

Introduction
Introduction
•Why Use JSP?

AdvantagesofJSP
Advantages
 
of
?
JSP
•Installing JSP

JSP

Architecture

JSP
 

Architecture
•JSP Processing

JSP
LifeCycle

JSP
 ‐
Life
?
Cycle
What is Java Server Pages?
•JavaServerPa
g
es
(
JSP
)
isatechnolo
gy
fordevelo
p
in
g
web
g
()
gy
pg
pagesthatsupportdynamiccontentwhichhelpsdevelopers
insertjavacodeinHTMLpagesbymakinguseofspecialJSP
tags
most
of
which
start
with
<
%
and
end
with
%
>
tags
,
most
of
which
start
with
<
%
and
end
with
%
>
.
•WebdeveloperswriteJSPsastextfilesthatcombineHTMLor
XHTMLcode,XMLelements,andembeddedJSPactionsand
commands.
•UsingJSP,youcancollectinputfromusersthroughwebpage
forms
present
records
from
a
database
or
another
source
forms
,
present
records
from
a
database
or
another
source
,
andcreatewebpagesdynamically.
Why Use JSP?
•PerformanceissignificantlybetterbecauseJSPallowsembeddingDynamic
Elements
in
HTML
Pages
itself
instead
of
having
a
separate
CGI
files
Elements
in
HTML
Pages
itself
instead
of
having
a
separate
CGI
files
.
•JSParealwayscompiledbeforeit'sprocessedbytheserverunlikeCGI/Perlwhich
requirestheservertoloadaninterpreterandthetargetscripteachtimethepage
is
requested
is
requested
.
•JavaServerPagesarebuiltontopoftheJavaServletsAPI,solikeServlets,JSPalso
hasaccesstoallthepowerfulEnterpriseJavaAPIs,includingJDBC,JNDI,EJB,JAXP
etc
etc
.
•JSPpagescanbeusedincombinationwithservletsthathandlethebusinesslogic,
themodelsupportedbyJavaservlettemplateengines.
Fill
JSP
i
itl
t
f
J
2
EE
lt
ltf
f
ti
l

Fi
na
ll
y,
JSP
i
san
i
n
t
egra
l
par
t
o
f
J
2
EE
,acomp
l
e
t
ep
l
a
tf
orm
f
oren
t
erpr
i
sec
l
ass
applications.ThismeansthatJSPcanplayapartinthesimplestapplicationsto
themostcomplexanddemanding.
AdvantagesofJSP
Advantages
 
of
?
JSP
•vs.ActiveServerPages(ASP):TheadvantagesofJSParetwofold.First,the
dynamic
part
is
written
in
Java
not
Visual
Basic
or
other
MS
specific
dynamic
part
is
written
in
Java
,
not
Visual
Basic
or
other
MS
specific
language,soitismorepowerfulandeasiertouse.Second,itisportableto
otheroperatingsystemsandnon‐MicrosoftWebservers.
•vs.PureServlets:Itismoreconvenienttowrite(andtomodify!)regular
HTMLthantohaveplentyofprintlnstatementsthatgeneratetheHTML.

vs
Server
Side
Includes
(SSI)
:
SSI
is
really
only
intended
for
simple

vs
.
Server

Side
Includes
(SSI)
:
SSI
is
really
only
intended
for
simple
inclusions,notfor"real"programsthatuseformdata,makedatabase
connections,andthelike.
•vs.JavaScript:JavaScriptcangenerateHTMLdynamicallyontheclientbut
canhardlyinteractwiththewebservertoperformcomplextaskslike
databaseaccessandimageprocessingetc.
•vs.StaticHTML:RegularHTML,ofcourse,cannotcontaindynamic
information.
Installing JSP
•Firsto
f
alldownloadJavaServerWebDevelo
p
mentKit
p
(JSWDK1.0.1)
from
http://java.sun.com/products/servlet/download.html
.JSWDK
comes
with
full
documentation
and
it
'
s
very
easy
to
install
comes
with
full
documentation
and
its
very
easy
to
install
.
•TheJSWDKistheofficialreferenceimplementationofthe
servlet2.1andJSP1.0specifications.Itisusedasasmall
stand‐aloneserverfortestingservletsandJSPpagesbefore
theyaredeployedtoafullWebserverthatsupportsthese
technologies
It
is
free
and
reliable
but
takes
quite
a
bit
of
technologies
.
It
is
free
and
reliable
,
but
takes
quite
a
bit
of
efforttoinstallandconfigure.
Other Servers that support JSP 
•Apache Tomcat.
Tomcat is the official reference implementation of the servlet2.2 and JSP 1.1 
s
p
ecifications. It can be used as a small stand‐alone server for testin
g
 servletsand 
p
g
JSP pages, or can be integrated into the Apache Web server.
•AllaireJRun.
JRun
isa
servlet
andJSPenginethatcanbepluggedintoNetscapeEnterpriseor
JRun
is
 
a
?
servlet
and
?
JSP
?
engine
?
that
?
can
?
be
?
plugged
?
into
?
Netscape
?
Enterprise
?
or
?
FastTrackservers, IIS, Microsoft Personal Web Server, older versions of Apache, 
O?Reilly?sWebSite, or StarNineWebSTAR. 
•New Atlanta?sServletExec.
ServletExecis a fast servletand JSP engine that can be plugged into most popular 
Web servers for Solaris, Windows, MacOS, HP‐UX and Linux. 
•Gefion'sLiteWebServer(LWS).
LWS is a small free Web server that supports 
servletsversion 2.2and JSP 1.1.
•WebSphere
. IBM's WebSpherevery large application server now implements JSP.
JSP ‐Architecture
JSP Processing
The
following
steps
explain
how
the
web
server
creates
the
web
The
following
steps
explain
how
the
web
server
creates
the
web
pageusingJSP:
•Aswithanormalpage,yourbrowsersendsanHTTPrequesttothe
webserver.

The
web
server
recognizes
that
the
HTTP
request
is
for
a
JSP
page
and
The
web
server
recognizes
that
the
HTTP
request
is
for
a
JSP
page
and
forwardsittoaJSPengine.ThisisdonebyusingtheURLorJSPpage
whichendswith.jspinsteadof.html.

The
JSP
engine
loads
the
JSP
page
from
disk
and
converts
it
into
a

The
JSP
engine
loads
the
JSP
page
from
disk
and
converts
it
into
a
servletcontent.Thisconversionisverysimpleinwhichalltemplate
textisconvertedtoprintln()statementsandallJSPelementsare
converted
to
Java
code
that
implements
the
corresponding
dynamic
converted
to
Java
code
that
implements
the
corresponding
dynamic
behaviorofthepage.
JSPProcessing(contd..)
JSP
 
Processing
?
(contd..)
•TheJSPenginecompilestheservletintoanexecutableclassand
forwardstheoriginalrequesttoaservletengine.
A
f
h
b
lld
h
l
i
ld
h
Sl

A
parto
f
t
h
ewe
b
serverca
ll
e
d
t
h
eserv
l
eteng
i
ne
l
oa
d
st
h
e
S
erv
l
et
classandexecutesit.Duringexecution,theservletproducesan
outputinHTMLformat,whichtheservletenginepassestotheweb
serverinsideanHTTPresponse.
•ThewebserverforwardstheHTTPresponsetoyourbrowserin
termsofstaticHTMLcontent.
•FinallywebbrowserhandlesthedynamicallygeneratedHTMLpage
insidetheHTTPresponseexactlyasifitwereastaticpage.
Steps explained with the help of a diagram
JSP ‐Life Cycle
PathsfollowedbyaJSPinitslifecycle
Paths
 
followed
?
by
?
a
?
JSP
??
in
?
its
?
life
?
cycle
The following are the paths followed by a JSP
1.Compilation
2.Initialization
3.Execution
4
Cleanup
4
.
Cleanup
•(1) JSP Compilation:
WhenabrowserasksforaJSP,theJSPenginefirstcheckstosee
whetheritneedstocompilethepage.Ifthepagehasneverbeen
compiled
or
if
the
JSP
has
been
modified
since
it
was
last
compiled
compiled
,
or
if
the
JSP
has
been
modified
since
it
was
last
compiled
,
theJSPenginecompilesthepage.
Thecompilationprocessinvolvesthreesteps:

Parsing
the
JSP
.
Parsing
the
JSP
.
•TurningtheJSPintoaservlet.
•Compilingtheservlet.
•(2) JSP Initialization
:
WhenacontainerloadsaJSPitinvokesthejspInit()methodbeforeservicing
anyrequests.Typicallyinitializationisperformedonlyonceandaswith
theservletinitmethod,yougenerallyinitializedatabaseconnections,
open
files
and
create
lookup
tables
in
the
jspInit
method
open
files
,
and
create
lookup
tables
in
the
jspInit
method
.
(3)JSP Execution:
•ThisphaseoftheJSPlifecyclerepresentsallinteractionswithrequests
untiltheJSPisdestroyed.
•WheneverabrowserrequestsaJSPandthepagehasbeenloadedand
initialized,theJSPengineinvokesthe
_
j
spService()methodintheJS
P
.
•The_jspService()methodofaJSPisinvokedonceperarequestandis
responsibleforgeneratingtheresponseforthatrequestandthis
method
is
also
responsible
for
generating
responses
to
all
seven
of
the
method
is
also
responsible
for
generating
responses
to
all
seven
of
the
HTTPmethodsie.GET,POST,DELETEetc.
(4)JSPCleanup:
(4)
 
JSP
?
Cleanup:
•ThedestructionphaseoftheJSPlifecyclerepresentswhenaJSPis
bein
g
removedfromuseb
y
acontaine
r
.
g
y
•ThejspDestroy()methodistheJSPequivalentofthedestroymethod
forservlets.