7.0 - Designing and Developing Thread-safe Servlets
7.1. Identify which attribute scopes are thread-safe:
7.1.1. local variables: yes, thread-safe
7.1.2. instance variables: not thread-safe, since a single
servlet instance may be handling multiple service requests
at any given time
7.1.3. class variables: not thread-safe, since multiple
servlets and/or service requests may try to access a class
variable concurrently
7.1.4. request attributes: yes, thread-safe, since the request
object is a local variable
7.1.5. session attributes: not thread-safe, since sessions
are scoped at the web application level, hence the same
session object can be accessed concurrently by multiple
servlets and their service requests
7.1.6. context attributes: not thread-safe, since the same
context object can be accessed concurrently by multiple
servlets and their service requests
7.2. Identify correct statements about differences between multi-threaded and single-threaded servlet models
7.2.1. multi-thread model servlet:
7.2.1.1. one servlet instance per registered name
7.2.1.2. for each servlet request, the server spawns a separate
thread which executes the servlet's service() method
7.2.1.3. must synchronize access to instance variables
7.2.2. single-thread model servlet:
7.2.2.1. has a pool of servlet instances per registered
name (depending on the server implementation, the pool size
may be configurable or not, and may be as little as one.)
7.2.2.2. guaranteed by server "that no two threads
will execute concurrently in the servlet's service method"
7.2.2.3. considered thread-safe and isn't required to synchronize
access to instance variables
7.2.2.4. does not prevent synchronization problems that
result from servlets accessing shared resources such as
static variables or objects outside the scope of the servlet
(e.g. ServletContext, HttpSession)
7.2.2.5. server might end up creating more instances than
the system can handle, e.g. each instance might have its
own db connection, hence in total there may be more db connections
than the db server can handle.