Web components, like the most objects, usually work and other objects to accomplish their tasks. Web components can does so by doing the following.
Using Private Helper Objects (for example, JavaBeans components).
Sharing objects that is attributes of a public scope.
Using a database.
Invoking other Web resources. The Java Servlet technology mechanisms-a Web component to invoke other Web resources is described ininvoking O Ther Web Resources.
17.3.1Using Scope Objects
Collaborating Web Components share information by means of objects that is maintained as attributes of four scope objects . You access these attributes by using the and methods of the getAttribute
setAttribute
class representing the scope. Table 17-2 lists the scope objects.
Table 17-2 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 this belongs to the session. See maintaining Client State. |
Request |
Subtype ofjavax.servlet.ServletRequest |
WEB components handling the request. |
Page |
javax.servlet.jsp.JspContext
|
The JSP page that creates the object. |
17.3.2Controlling Concurrent Access to Shared Resources
In a multithreaded server, the shared resources can be accessed concurrently. In addition to 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.
-
Multiple Web accessing objects stored in the web context.
-
Multiple Web accessing objects stored in a session.
-
Multiple threads within a Web component accessing instance variables. A web container would typically create a thread to handle each request. To ensure. A servlet instance handles only one request at a time, a servlet can implement the singlethreadmodel< /code> interface. If a servlet implements this interface, no threads would execute concurrently in the Servlet's service method. A web container can implement this guarantee by synchronizing access to a single instance of the Servlets or by maintaining A pool of Web Component instances and dispatching each new request to a free instance. This interface does not prevent synchronization problems the result from Web Components ' accessing shared resources, such As static class variables or external objects.
When the resources can be accessed concurrently, they can is used in an inconsistent fashion. You prevent the controlling the access using the synchronization techniques described in the Threads lesson at http://docs.oracle.com/javase/tutorial/essential/concurrency/
.
Java EE Learning path-sharing information