Servlet Scope Objects: Web Context and Session, Slides of Java Programming

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

2011/2012

Uploaded on 07/13/2012

ahmad.ali
ahmad.ali 🇮🇳

3.7

(3)

78 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Servlets
1
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Servlet Scope Objects: Web Context and Session and more Slides Java Programming in PDF only on Docsity!

Servlets

Scope

Objects

Web

context

(ServletConext)

  • Accessible

from

Web

components

within

a

Web

context

Session

  • Accessible

from

Web

components

handling

a

request

that

belongs

to

the

session

Request

  • Accessible

from

Web

components

handling

the

request

Page

  • Accessible

from

JSP

page

that

creates

the

object

Web

context

  • javax.servlet.ServletContext

Session

  • javax.servlet.http.HttpSession

Request

  • subtype

of

javax.servlet.ServletRequest:

javax.servlet.http.HttpServletRequest

Page

  • javax.servlet.jsp.PageContext

Used

by

servers

to

  • Set

and

get

context

‐ wide

(application

‐ wide)

object

valued

attributes

  • Get

request

dispatcher

To

forward

to

or

include

web

component

  • Access

Web

context

‐ wide

initialization

parameters

set

in

the

web.xml

file

  • Access

Web

resources

associated

with

the

Web

context

  • Log– Access

other

misc.

information

Context

wide

scope

  • Shared

by

all

servlets

and

JSP

pages

within

a

"web

application"

Why

it

is

called

“web

application

scope”

– A

"web

application

is

a

collection

of

Servlets and

content

installed

under

a

specific

subset

of

the

server's

URL

namespace

and

possibly

installed

via

a

*.war

file

All

servlets

in

Bookstore

web

application

share

same

ServletContext

object.

  • There

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

  • init

(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(""

^

""</p> <p>+^</p> <p>messages.getString("TitleBookDescription")</p> <ul> <li></li> </ul> <p>^</p> <p>""); ^

//^

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

  • Example:

Online

shopping

cart

Yet,

HTTP

is

stateless

HttpSession maintains

client

state

  • Used

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

URL

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

URL

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.