10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

■ Multiple threads within a web component accessing instance variables. A web container will<br />

typically create a thread to handle each request. If you want to ensure that a servlet instance<br />

handles only one request at a time, a servlet can implement the SingleThreadModel<br />

interface. If a servlet implements this interface, you are guaranteed that no two threads will<br />

execute concurrently in the servlet’s service method. A web container can implement this<br />

guarantee by synchronizing access to a single instance of the servlet, or by maintaining a<br />

pool of web component instances and dispatching each new request to a free instance. This<br />

interface does not prevent synchronization problems that result from web components<br />

accessing shared resources such as static class variables or external objects. In addition, the<br />

Servlet 2.4 specification deprecates the SingleThreadModel interface.<br />

When resources can be accessed concurrently, they can be used in an inconsistent fashion. To<br />

prevent this, you must control the access using the synchronization techniques described in the<br />

Threads lesson in <strong>The</strong> <strong>Java</strong> <strong>Tutorial</strong>, Fourth Edition, by Sharon Zakhour et al. (Addison-Wesley,<br />

2006).<br />

<strong>The</strong> preceding section showed five scoped attributes shared by more than one servlet: bookDB,<br />

cart, currency, hitCounter, and orderCounter. <strong>The</strong> bookDB attribute is discussed in the next<br />

section. <strong>The</strong> cart, currency, and counters can be set and read by multiple multithreaded servlets.<br />

To prevent these objects from being used inconsistently, access is controlled by synchronized<br />

methods. For example, here is the Counter class, located at<br />

tut-install/javaeetutorial5/examples/web/bookstore1/src/java/com/sun/bookstore1/util/:<br />

public class Counter {<br />

private int counter;<br />

public Counter() {<br />

counter = 0;<br />

}<br />

public synchronized int getCounter() {<br />

return counter;<br />

}<br />

public synchronized int setCounter(int c) {<br />

counter = c;<br />

return counter;<br />

}<br />

public synchronized int incCounter() {<br />

return(++counter);<br />

}<br />

}<br />

Accessing Databases<br />

Sharing Information<br />

Data that is shared between web components and is persistent between invocations of a web<br />

application is usually maintained by a database. Web components use the <strong>Java</strong> Persistence API<br />

to access relational databases. <strong>The</strong> data for Duke’s Bookstore is maintained in a database and is<br />

Chapter 4 • <strong>Java</strong> ServletTechnology 107

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!