The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

Sharing Information

Web components, like most objects, usually work with other objects to accomplish their tasks. There are several ways they can do this. They can use private helper objects (for example, JavaBeans components), they can share objects that are attributes of a public scope, and they can invoke other web resources. The Java Servlet technology mechanisms that allow a web component to invoke other web resources are described in Invoking Other Web Resources.

Scope Objects

Collaborating web components share information via objects maintained as attributes of four scope objects. These attributes are accessed with the [get|set]Attribute methods of the class representing the scope. Table 12 lists the scope objects.

Table 12 Scope Objects 
Scope Object
Class
Accessible From
web context
javax.servlet.
ServletContext

Web components within a Web context. See Accessing the Web Context.
session
javax.servlet.
http.HttpSession

Web components handling a request that belongs to the session. See Maintaining Client State.
request
subtype of
javax.servlet.
ServletRequest

Web components handling the request.
page
javax.servlet.
jsp.PageContext

The JSP page that creates the object. See JavaServer PagesTM Technology.

Figure 13 shows the scoped attributes maintained by the Duke's Bookstore application.

Figure 13 Duke's Bookstore Scoped Attributes

Controlling Concurrent Access to Shared Resources

In a multithreaded server, it is possible for shared resources to be accessed concurrently. Besides scope object attributes, shared resources include in-memory data such as instance or class variables and external objects such as files, database connections, and network connections. Concurrent access can arise in several situations:

When resources can be accessed concurrently, they can be used in an inconsistent fashion. To prevent this, you must control the access using the synchronization techniques described in the Threads lesson in the Java Tutorial.

The Duke's Bookstore database helper object BookDB maintains a database connection that is referenced by an instance variable named con. Only one servlet can use connection at a time. Since the servlets accessing this object are multithreaded, access to the con variable is controlled by the synchronized methods getConnection and releaseConnection.

private Connection con;
private boolean conFree = true;
public BookDB () throws Exception {
	try  {               
		InitialContext ic = new InitialContext();
		DataSource ds = (DataSource) ic.lookup(dbName);
		con = ds.getConnection();     
	} catch (Exception ex) {
		throw new Exception(
			"Couldn't open connection to database: " +
				ex.getMessage());
	}
}
protected synchronized Connection getConnection() {
	while (conFree == false) {
		try {
			wait();
		} catch (InterruptedException e) {
			...
		}
	}
	conFree = false;
	notify();
	return con;
}

protected synchronized void releaseConnection() {
	while (conFree == true) {
		try {
			wait();
		} catch (InterruptedException e) {
			...
		}
	}
	conFree = true;
	notify();
} 
Home
TOC
PREV TOC NEXT Search
Feedback