Servlets: Part 2 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Servlets: Part 2

Description:

c.setAttribute('A:number', new Integer(3)); public class B extends HttpServlet ... session.setAttribute('a', new Float(i )); public class B extends HttpServlet ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 19
Provided by: wwwcsifC
Category:
Tags: part | servlets

less

Transcript and Presenter's Notes

Title: Servlets: Part 2


1
Servlets Part 2
  • Stoney Jackson
  • jacksoni_at_cs.ucdavis.edu
  • http//wwwcsif.cs.ucdavis.edu/jacksoni

2
Contexts
  • Places where servlets can store
  • and retrieve information.

3
Context Information
4
Servlet Context
  • 1 per web-application (not per server)

Server
yourWebApp
myWebApp
5
Accessing Servlet Context
  • public class A extends HttpServlet
  • protected void doGet(...) throws ...
  • ServletContext c getServletContext()
  • c.setAttribute(Anumber, new Integer(3))
  • ...
  • public class B extends HttpServlet
  • protected void doPost(...) throws ...
  • ServletContext c getServletContext()
  • Object o c.getAttribute(Anumber)
  • Integer i (Integer) o
  • ...

TYPE SAFETY IS YOUR RESPONSIBILITY!
ClassCastException?
Not this time
6
Request Context
  • 1 per request

Request
Request
Request
7
Accessing Request Context
  • public class A extends HttpServlet
  • protected void doGet(HttpServletRequest
    request,...)
  • throws ...
  • String s request.getParameter(two)

For forms and query strings
Lots of other request related stuff,see
HttpServletRequest and ServletRequest
8
Session Context
  • 1 per client

9
Session Context (cont.)
  • 1 per client per web-application

A
B
10
Accessing Session Context
  • public class A extends HttpServlet
  • static int i 0
  • protected void doGet(HttpServletRequest
    request, ...)
  • throws ...
  • HttpSession session request.getSession()
  • session.setAttribute(a, new Float(i))
  • public class B extends HttpServlet
  • protected void doPost(...) throws ...
  • HttpSession session request.getSession()
  • Integer k (Integer) session.getAttribute(a
    )

TYPE SAFETY IS YOUR RESPONSIBILITY!
ClassCastException!!!
11
Contexts
  • Servlet
  • 1 per web-application
  • use for sharing application specific information
    and resources
  • Request
  • 1 per request
  • use for forms and query strings
  • Session
  • 1 per client per web-application
  • use for sharing client specific information and
    resources

12
Race Condition
  • A race condition is when two or more processes or
    threads concurrently access the same data and the
    outcome depends on the order in which their
    accesses take place.

32
write 12
read
RACE CONDITION
Reads 32 or 12?
32
write 12
write 6
RACE CONDITION
12 or 6?
32
read
read
13
Servlets are Multi-Threaded
  • Thread per request
  • Shared data
  • Potential Race Conditions

14
Bank Scenario
  • public class Withdrawal extends HttpServlet
  • protected void doGet(...) throws ...
  • float amount Float.parseFloat(request.getPro
    perty(amount))
  • ServletContext c getServletContext()
  • Float balance (Float) c.getAttribute(balanc
    e)
  • amount - balance.floatValue()
  • c.setAttribute(balance, amount)
  • public class Deposit extends HttpServlet
  • protected void doPost(...) throws ...
  • float amount Float.parseFloat(request.getPro
    perty(amount))
  • ServletContext c getServletContext()
  • Float balance (Float) c.getAttribute(balanc
    e)
  • amount balance.floatValue()
  • c.setAttribute(balance, amount)

15
Wheres the Race?
A
B
125
25
Withdrawal
Deposit
  • Aget 100
  • Bget 100
  • Aset -25
  • Bset 125
  • Nice bank!
  • Aget 100
  • Bget 100
  • Bset 125
  • Aset -25
  • I take it back.

get
set
get
set
100
16
synchronized
  • Object o1 ...
  • synchronized(o1)
  • // only one thread allowed in this block ...
  • synchronized(o1)
  • // and in this block

Only one thread in either block!
17
synchronizedis Keyed on Instance
  • Object o1 ...
  • synchronized(o1)
  • // only one thread allowed in this block ...
  • o1 new Object()
  • synchronized(o1)
  • // this is a different synchronizedd block

18
Fixing the Bank
  • public class Withdrawal extends HttpServlet
  • protected void doGet(...) throws ...
  • float amount Float.parseFloat(request.getPro
    perty(amount))
  • ServletContext c getServletContext()
  • synchronized(c)
  • Float balance (Float) c.getAttribute(balan
    ce)
  • amount balance.floatValue()
  • c.setAttribute(balance, amount)
  • public class Deposit extends HttpServlet
  • protected void doPost(...) throws ...
  • float amount Float.parseFloat(request.getPro
    perty(amount))
  • ServletContext c getServletContext()
  • synchronized(c)
  • Float balance (Float) c.getAttribute(balan
    ce)
  • amount - balance.floatValue()
  • c.setAttribute(balance, amount)
Write a Comment
User Comments (0)
About PowerShow.com