





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of servlets and the use of scope objects in java web development. It covers the four types of scope objects - web context, session, request, and page - and their respective uses and access methods. The document also discusses the importance of the servletcontext and httpsession objects.
Typology: Slides
1 / 45
This page cannot be seen from the preview
Don't miss anything!






































Context
‐ wide
scope
by
all
servlets
and
pages
within
a
"web
application"
Why
it
is
called
“web
application
scope”
"web
application
is
a
collection
of
Servlets and
content
installed
under
a
specific
subset
of
the
server's
namespace
and
possibly
installed
via
a
*.war
file
All
servlets
in
Bookstore
web
application
share
same
ServletContext
object.
is
one
ServletContext
object
per
"web
application"
per
Java
Virtual
Machine
Within
your
servlet
code,
call
getServletContext()
Within
your
servlet
filter
code,
call
getServletContext()
The
ServletContext
is
contained
in
ServletConfig object,
which
the
Web
server
provides
to
a
servlet
when
the
servlet
is
initialized
(ServletConfig servletConfig)
in
Servlet
interface
version="1.0"encoding="UTF
‐8"?>
<web
‐app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema
‐instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web
‐app_2_4.xsd"
version="2.4"> <display
‐name>or</display
‐name>
<context
‐param>
<param
‐name>configFilePath</param
‐name>
<param
‐value>E:\work\MobilinkOR\MobilinkOR\or\WEB
‐
INF\jdbcProps.txt</param
‐value>
</context
‐param>
public
class
CatalogServlet extends
HttpServlet {
private
BookDB bookDB;
public
void
init()
throws
ServletException {
Get
context
‐ wide
attribute
value
from
ServletContext
object
bookDB =
(BookDB)getServletContext().
getAttribute("bookDB"); if
(bookDB ==
null)
throw
new
UnavailableException("Couldn't
get
database.");
^
public
void
doGet (HttpServletRequest request,
^
HttpServletResponse response) ^
throws
ServletException,
IOException {
^
HttpSession session
=^
request.getSession(true);
^
ResourceBundle messages
=^
(ResourceBundle)session.getAttribute("messages");
^
//^
set
headers
and
buffer
size
before
accessing
the
Writer
^
response.setContentType("text/html"); ^
response.setBufferSize(8192); ^
PrintWriter out
=^
response.getWriter();
^
//^
then
write
the
response
^
out.println(""
^
"
+^
messages.getString("TitleBookDescription")
^
"
//^
Get
the
dispatcher;
it gets
the
banner
to
the
user
^
RequestDispatcher dispatcher
=
^
getServletContext().getRequestDispatcher("/banner"); ^
if^ (dispatcher
!=
null)
^
dispatcher.include(request,
response);
^
...
Session
(HttpSession)
Need
a
mechanism
to
maintain
client
state across
a
series
of
requests
from
a
same
user
(or
originating
from
the
same
browser)
over
some
period
of
time
Online
shopping
cart
Yet,
HTTP
is
stateless
HttpSession maintains
client
state
by
Servlets
to
set
and
get
the
values
of
session
scope
attributes
You
can
use
HTTP
cookies
to
store
information
about
a
shopping
session,
and
each
subsequent
connection
can
look
up
the
current
session
and
then
extract
information
about
that
session
from
some
location
on
the
server
machine.
This
is
an
excellent
alternative,
and
is
the
most
widely
used
approach.
However,
even
though
servlets
have
a
high
‐level
and
easy
‐to
‐use
interface
to
cookies,
there
are
still
a
number
of
relatively
tedious
details
that
need
to
be
handled:
^
Extracting
the
cookie
that
stores
the
session
identifier
from
the
other
cookies
(there
may
be
many,
after
all),
^
Setting
an
appropriate
expiration
time
for
the
cookie
(sessions
interrupted
by
24
hours
probably
should
be
reset).
^
Associating
information
on
the
server
with
the
session
identifier
(there
may
be
far
too
much
information
to
actually
store
it
in
the
cookie,
plus
sensitive
data
like
credit
card
numbers
should
never
go
in
cookies).
You
can
append
some
extra
data
on
the
end
of
each
that
identifies
the
session,
and
the
server
can
associate
that
session
identifier
with
data
it
has
stored
about
that
session.
This
is
also
an
excellent
solution,
and
even
has
the
advantage
that
it
works
with
browsers
that
don't
support
cookies
or
where
the
user
has
disabled
cookies.
However,
it
has
most
of
the
same
problems
as
cookies,
namely
that
the
server
‐side
program
has
a
lot
of
straightforward
but
tedious
processing
to
do.
In
addition,
you
have
to
be
very
careful
that
every
returned
to
the
user
(even
via
indirect
means
like
Location
fields
in
server
redirects)
has
the
extra
information
appended.
And,
if
the
user
leaves
the
session
and
comes
back
via
a
bookmark
or
link,
the
session
information
can
be
lost.