The Java EE 6 Tutorial Sharing Information
2015-08-29 08:59
483 查看
Web components, like most objects, usually work with other objects to accomplish their tasks. Web components can do so by
Using private helper objects (for example, JavaBeans components).
Sharing objects that are attributes of a public scope.
Using a database.
Invoking other web resources.
Table 15-2 Scope Objects
Concurrent access can arise in several situations:
Multiple web components accessing objects stored in the web context.
Multiple web components accessing objects stored in a session.
Multiple threads within a web component accessing instance variables. A web container will typically create a thread to handle each request. To ensure that a servlet instance handles only one request at a time, a servlet can implement the
When resources can be accessed concurrently, they can be used in an inconsistent fashion. You prevent this by controlling the access using the synchronization techniques.
Previous Contents Next
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices
Using private helper objects (for example, JavaBeans components).
Sharing objects that are attributes of a public scope.
Using a database.
Invoking other web resources.
Using Scope Objects
Collaborating web components share information by means of objects that are maintained as attributes of four scope objects. You access these attributes by using thegetAttributeand
setAttributemethods of the class representing the scope. Table 15-2 lists the scope objects.
Table 15-2 Scope Objects
Scope Object | Class | Accessible from |
---|---|---|
Web context | javax.servlet.ServletContext | Web components within a web context. |
Session | javax.servlet.http.HttpSession | Web components handling a request that belongs to the session. |
Request | Subtype of javax.servlet.ServletRequest | Web components handling the request. |
Page | javax.servlet.jsp.JspContext | The JSP page that creates the object. |
Controlling Concurrent Access to Shared Resources
In a multithreaded server, 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 components accessing objects stored in the web context.
Multiple web components accessing objects stored in a session.
Multiple threads within a web component accessing instance variables. A web container will typically create a thread to handle each request. To ensure that a servlet instance handles only one request at a time, a servlet can implement the
SingleThreadModelinterface. If a servlet implements this interface, no two threads will execute concurrently in the servlet’s
servicemethod. A web container can implement this guarantee by synchronizing access to a single instance of the servlet 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 that result from web components’ accessing shared resources, such as static class variables or external objects.
When resources can be accessed concurrently, they can be used in an inconsistent fashion. You prevent this by controlling the access using the synchronization techniques.
Previous Contents Next
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices
相关文章推荐
- java静态块,非静态块,构造函数
- 序列化和反序列化
- Javaweb 初期Html网页设计基础
- java多线程(十)使用线程安全的集合
- Java 子类初始化父类的构造方法 成员变量不支持多态
- eclipse 异常 Subversion Native Library Not Available解决方案
- The Java EE 6 Tutorial
- Java解析XML文件方法之一(DOM)
- 重温java基础
- HPday05--java--Scanner输入非数字处理
- java获得ip地址
- Some projects cannot be imported because they already exist in the workspace
- Using Struts 2 Tags
- Hello World Using Struts 2
- mybits最基本的例子---------实现增删改查
- hibernate、java、数据库对应类型
- 【SSH快速进阶】——struts2调用action的三种方式
- 细说java.util.Timer
- Java IO编程 学习笔记 (其二:简易记事本)
- Java反射机制调用私有方法