The document discusses various topics related to web technology and Java web development. It introduces web applications and their typical components like servlets and JSPs. It explains that servlets are Java classes that can be invoked by a URL, while JSPs allow mixing Java code in HTML documents. The document also discusses the servlet specification, web containers, and their role in managing web applications and isolating components.
The document discusses various topics related to web technology and Java web development. It introduces web applications and their typical components like servlets and JSPs. It explains that servlets are Java classes that can be invoked by a URL, while JSPs allow mixing Java code in HTML documents. The document also discusses the servlet specification, web containers, and their role in managing web applications and isolating components.
What
have
we
covered!
• The
Internet
Architecture
[reading
assignment
from
Computer
Networking:
top
down
approach
]
• Maven
IntroducEon
and
installaEon
• Running
a
Project
Using
Maven
Agenda
• IntroducEon
to
Web
Technology
• What
Java
offers
for
Web?
• The
Servlet
SpecificaEon
• Web
Containers
• Context
Path
• ApplicaEon
packaging
• Paths:
how
to
connect
• What
is
a
context
• Servlet
Context
• Servlet
Config
• Page
context
• Servlet
and
JSP
Lifecycle
• Deployment
Descriptor
Web
Technology:
Intro
• A
program
that
you
can
be
invoked
through
internet
by
using
two
different
kinds
of
soQware
known
as
:
– Web
Server
(server
side)
– Browser
(client
side)
• The
output
is
what
we
know
as
“WEBSITES”
• Websites
can
be
developed
in
different
programming
languages
such
as
C,
Perl,
Java
and
.Net
etc.
• The
client
side
i.e.
inside
a
browser
is
dominated
by
another
language
we
know
as
JavaScript
Source
Code
of
a
Website!
• Have
you
tried
to
see
what's
behind
the
flashy
website.
<html>
<head>
<Etle>My
Web
Page</Etle>
</head>
<body>……</body>
</html>
Web
Technology…again!
• A
program
that
you
can
be
invoked
through
internet
by
using
two
different
kinds
of
soQware
known
as
:
– Web
Server
(server
side)
– Browser
(client
side)
• The
output
is
what
we
know
as
“WEBSITES”
• Websites
can
be
developed
in
different
programming
languages
such
as
C,
Perl,
Java
and
.Net
etc.
• The
client
side
i.e.
inside
a
browser
is
dominated
by
another
language
we
know
as
JavaScript.
• The
programs
wriaen
in
these
languages
product
HTML/JavaScript
content
that
browsers
understand
and
display/handle
it
accordingly.
What
does
Java
Offer..
• Java
specificaEons
are
quite
rich
following
is
the
list
of
specificaEon
that
dominate
the
enterprise
arena:
– Java
Servlet
– JavaServer
Pages
(JSP)
– Java
Server
Pages
Standard
Tag
Library(JSTL)
– Expression
Language(EL)
– Web
Services
– Java
Persistence(JPA)
History
of
Java
Specs
• In
1996,
Java
programming
language
was
sEll
young
and
upcoming
technology
and
got
immediate
aaenEon
by
the
developers
community.
• Java
Beans
the
most
important
Java
Spec
(POJO)
was
released
the
same
year.
• As
the
development
grew,
sophisEcated
applicaEons
oQen
required
services
such
as
transacEon
,
security
and
distributed
compuEng.
• Therefore
in
March
1998,
Sun
published
a
spec
for
Enterprise
Java
Beans
(EJB)
which
pulled
focus
on
ServerSide
Components.
• The
components
were
able
to
use
these
enterprise
services
provisioned
by
the
container.
Web
ApplicaEon
• Simply
put,
Web
ApplicaEons
can
be
disEnguished
as
Apps
invoked
by
a
URL.
• Web
applicaEon
is
a
collecEon
of
components,
chosen
or
designed
to
fulfill
a
desired
purpose.
• Web
ApplicaEon
can
contain
both
staEc
and
dynamic
components,
following
is
normally
what
you
will
commonly
observe
to
be
part
of
it:
– Servlets,
JSPs,
XML
files,
JavaBeans
What
is
a
Servlet
• A
Java
class
that
can
be
invoked
by
a
URL.
}
The
Servlet
SpecificaEon
What
is
a
JSP?
• It’s
essenEally
a
servlet
but
you
can
mix
java
code
inside
an
HTML
document.
<html>
<head>
<Etle>My
Web
Page</Etle>
</head>
<body>
<%
•
Date
date
=
new
Date();
•
out.print(
"<h2
align=\"center\">"
+date.toString()+"</h2>");
• %>
</body>
</html>
Web
Container
• Web
Container
is
the
soQware
where
all
web
applicaEons
are
managed
and
executed.
• A
Web
Container
handles
a
lot
of
menial
tasks
for
developers.
In
order
to
provision
services
such
as
security
and
filtering,
pooling,
caching
Container
manages
applicaEons
as
“Sand
Boxes”.
• Each
Java
EE
module
has
its
own
container,
so
separate
ones
for
EJBs,
Applets
and
ApplicaEons,
obviously
we
are
interested
in
Web
Containers.
• The
container
is
for
Java
web
applicaEons
what
the
JVM
(Java
Virtual
Machine)
is
for
local
running
Java
applicaEons.
External
Requests
All
the
external
requests
are
managed
by
the
container
Component
InteracEon
The
components
(Wars)
also
communicate
through
container
interface
ApplicaEon
IsolaEon
• It’s
the
Web
Container’s
job
to
isolate
dynamic
modules,
Servlets,
JSPs
and
JavaBeans
from
each
other.
(Virtual
Divide)
• In
the
below
diagram,
Router
class
is
two
different
apps.
It
might
be
the
same
which
makes
VD
less
significant.
• VD
is
parEcularly
useful
for
different
classes
having
the
same
name.
• VD
here
means
that
app1Router.equals(app2Router)
should
result
false.
• Study
J2EE
class
loaders
from
here.
Component
Comm.
App
Packaging:
WAR
• Java
EE
suggests
different
packaging
standards
for
different
kinds
of
applicaEon
e.g.
EAR
for
enterprise
applicaEons
etc.
• Here
we
are
interested
in
only
Web
Archive
(WAR).
WARs
are
very
similar
to
Jar
files
but
with
a
different
internal
structure
as
shown
below.
• Nothing
to
remember,
just
create
a
dynamic
web
project
and
study
the
directory
structure
and
arEfacts
placed
in
it.
• Lets
discuss
each
directory
in
the
next
slide.
App
Packaging:
WAR……..(contd.)
• The
name
of
the
project
created
in
eclipse
will
become
the
topmost
directory
name
i.e.
name
of
the
WebApp.
(HelloWebApp.war).
• The
content
can
be
majorly
categorized
as
public
and
private.
• The
private
content
is
inside
the
WEB-‐INF
directory
and
is
mainly
used
by
the
container
and
WebApp
itself.
• The
private
content
can
be
configuraEon
files,
passwords,
DB
connecEon
which
should
not
at
all
be
displayed
to
the
client
at
any
condiEon.
• It
can
also
contain
page
fragments
such
as
banner,
footer
etc.
• WEB-‐INF
hosts
Deployment
Descriptor
i.e.
web.xml
WEB-‐INF
StaEc
(public)
Content:
URLs
Dynamic
Content:
URLs
Context
Root
• The
context
root
is
where
the
iniEal
differenEaEon
occurs
between
various
applicaEons
and
is
idenEfied
as
“/”.
• The
basic
of
among
all
the
responsibiliEes
is
to
differenEate
paths
through
which
each
applicaEon
will
be
called.
• It
defaults
to
applicaEon
directory
or
archive
(war)
structure,
for
example
applicaEon.war
which
contains
the
hello
directory
where
your
JSPs
are,
the
URL
to
such
applicaEon
will
be
/applicaEon/hello.
Paths
• The
context
root
is
unique
for
every
applicaEon,
it
also
acts
as
URL
path
from
the
root
of
the
Java
EE
Webserver.
For
example
if
a
Web
Server’s
default
path
was
set
at
hap://domain.ext/
and
the
context
for
an
applicaEon
/shop/catalogue
then
the
full
URL
is
hap://domain.ext/shop/catalogue.
There
are
two
types
of
URLs.
• RelaEve
Paths:
Doesn’t
begin
with
“/”
and
are
relaEve
to
Servlet/ JSP
e.g.
config/data.xml
called
from
a
servlet
locaEon
/shop/ catalogue
will
result
in
/shop/catalogue/config/data.xml.
• Context-‐relaEve
Paths:
These
are
applicaEon
context
paths
and
are
not
relaEve
to
locaEon.
Such
paths
begin
with
“/”
so
a
path
config/ data.xml
specified
at
/shop/catalogue/
will
be
interpreted
as
/ config/data.xml
from
applicaEon
root.
Reusing
the
above
domain
name
it
would
become
hap://domain.ext/config/data.xml.
Context:
What
it
is?
• A
Context
represents
your
environment.
It
represents
the
state
surrounding
where
you
are
in
your
system.
• In
today’s
modern
web
app,
many
components
work
together
to
accomplish
a
goal.
You
have
models,
controllers,
and
views.
You
have
parameters
and
aaributes.
You
have
helper
classes.
But
how
do
you
Ee
the
pieces
together?
How
do
you
let
components
share
informaEon?
How
do
you
hide
informaEon?
How
do
you
make
informa1on
thread-‐safe?
• The
answer
is
context.
You
can
have
three
types
of
context
in
Web
App
development
in
Java.
– ServletContext
and
Context
root
(/)
– ServletConfig
– PageContext
Servlet
Context
• ServletContext
is
the
programmaEc
idenEficaEon
of
ApplicaEon
Context.
The
ServletContext
object
is
used
to
provide
all
servlets
in
an
applicaEon
with
a
view
to
that
applicaEon.
• For
each
applicaEon
we
have
exactly
one
ServletContext
object.
• It
can
be
used
to
iniEalize
servlets
with
required
values
at
startups.
Below
is
an
example
how
you
specify
context
Aaributes:
• Please
note
the
tag
name
its
context-‐param.
ServletConfig
• A
ServletConfig
object
is
used
by
a
Servlet
Container
to
pass
the
iniEalizaEon
informaEon
to
a
servlet
during
its
iniEalizaEon.
• IniEalizaEon
parameters
specified
inside
<servlet>
tag
as
<init-‐ param>
enable
ServletConfig
to
load
values
from
as
shown
below.
<servlet>
……
<init-‐param>
<param-‐name>NameOfTheInitParameter1</param-‐name>
<param-‐value>ValueOfTheInitParameter1</param-‐value>
</init-‐param>
...
</servlet>
PageContext
• Used
in
JSP
and
purports
as
plaxorm
neutral
context
for
accessing
data
about
JSP
pages.
PageContext
subclasses
JSPContext
class.
• Both
of
the
above
objects
are
used
as
storage
for
page-‐scoped
aaributes.
• They
are
provisioned
with
useful
uElity
methods
to
access
objects
in
different
scopes
such
as
page,
session,
request
and
applicaEon.
• We
will
discuss
more
about
scopes
later.
Lifecycle
of
a
servlet
Lifecycle
of
a
JSP
Apache
Tomcat
A
Web
App
Deployment
Descriptor(Web.xml
<?xml
version="1.0"
encoding="UTF-‐8"?>
<web-‐app
xmlns:xsi="hRp://www.w3.org/2001/XMLSchema-‐instance"
xmlns="hRp://java.sun.com/ xsi:schemaLoca1on="hRp://java.sun.com/xml/ns/javaee
hRp://java.sun.com/xml/ns/javaee/web-‐ app_2_5.xsd"
id="WebApp_ID"
version="2.5">
GET
or
POST
A
request
can
be
of
two
types:
How
will
I
Know!
• You
can
know
it
from
three
places:
– The
URL
(Google
anything)
– The
form’s
Method
aaribute
<form
acEon=“/servlet1”
method=“get”>
– Servlet’s
methods:
doGet
is
called
for
GET
request
and
vice
versa.
out.println("<html>");
out.println("<head>");
out.println("<Etle>Any
thing
can
be
concatenated
here</Etle>");
out.println("</head>");
out.println("<body
bgcolor=\"white\"></body>");
out.println("</html>");
}
}
Exercise
1
• Develop
an
ApplicaEon
that
displays
an
HTML
file
when
the
applicaEon
is
loaded.
• The
above
page
should
display
a
form
that
uses
two
text
boxes
and
two
buaon
(submit
and
reset).
(Say
user
id
and
password)
• This
form
should
submit
any
values
entered
when
submit
buaon
is
pressed.
You
have
to
menEon
servlet
path
in
the
acEon
aaribute.
• Add
a
servlet
that
will
accept
these
values
and
display
it
on
the
console.
(display
userid
and
password)
Exercise
2
• Develop
an
ApplicaEon
that
displays
an
HTML
file
when
the
applicaEon
is
loaded.
• The
above
page
should
display
a
form
that
uses
two
text
boxes
(for
entering
two
numbers
say
number1
and
number2)
and
two
buaon
(submit
and
reset).
The
Submit
Buaon
should
display
Add
as
its
capEon.
• This
form
should
submit
any
values
entered
when
submit
buaon
is
pressed.
You
have
to
menEon
servlet
path
in
the
acEon
aaribute.
• Add
a
servlet
to
the
project
that
will
accept
these
values
and
the
sum
returned
back
to
the
client
(browser).
Hint:
use
Interger.parseInt()
method
to
convert
a
string
into
an
int.
Exercise
3
Create
a
servlet
that
makes
a
bulleted
list
of
four
random
numbers.
•
Hint
1:
you
use
Math.random()
to
output
a
random
number
in
Java.
•
Hint
2:
you
make
a
bulleted
list
in
HTML
as
follows
<UL>
<LI>List
item
1
<LI>List
item
2
...
</UL>
Exercise
5
• Create
a
servlet
that
uses
a
loop
to
output
an
HTML
table
with
3
rows
and
3
columns.
You
are
also
required
to
display
a
value
in
each
cell
e.g.
In
Row1
and
Col1
display
11
for
Row1
and
Col2
display
12
and
so
on.