260 likes | 453 Views
The Servlet Container Model. 鄧姚文 joseph.deng@gmail.com. Outline. Initializing ServletContext Adding and listening to scope attributes Servlet life-cycle events and listeners Adding listeners in the deployment descriptor Web applications in a distributed environment. 簡介.
E N D
The Servlet Container Model 鄧姚文 joseph.deng@gmail.com
Outline Initializing ServletContext Adding and listening to scope attributes Servlet life-cycle events and listeners Adding listeners in the deployment descriptor Web applications in a distributed environment
簡介 • Within a web application, all of the servlets share the same environment • javax.servlet.ServletContext • The Servlet API also defines interfaces that allow the servlets and the servlet container to interact with each other
Initializing ServletContext Every web application has exactly one instance of javax.servlet.ServletContext The context is initialized at the time that the web application is loaded
Adding and Listening to Scope Attributes • Type of scopes • Request: HttpServletRequest • Session: HttpSession • Application: ServletContext • Add/remove attributes • Listening to events related to attributes
Listening to Request Attribute Events Create a class that implements the ServletRequestAttributeListener interface This class will respond to attribute events as long as the request hasn’t left the application’s scope
Listening to Session Attribute Events • HttpSessionAttributeListener • whenever attributes are added to, removed from, or replaced • HttpSessionBindingListener • whenever attributes are added to or removed from a session • HttpSessionActivationListener • when a session is created or destroyed
Listening to Context Attribute Events The ServletContextAttributeListener interface is used to receive notifications about the changes to the attribute list of a servlet context
Servlet Life-Cycle Events and Listeners • javax.servlet.ServletContextListener • Allows a developer to know when a servlet context is initialized or destroyed
Adding Listeners inThe Deployment Descriptor <!ELEMENT web-app (icon?, display-name?, description?, distributable?, context-param*, filter*, filter-mapping*, listener*, servlet*, servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?, error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,login-config?, security-role*, env-entry*, ejb-ref*, ejb-local-ref*)>
Adding Listeners inThe Deployment Descriptor display-name—Defines a short name that can be used by development tools, such as IDEs description—Defines the usage and any important information that the developer might want to convey to the deployer distributable—Indicates that the application can be distributed across multiple JVMs
Adding Listeners inThe Deployment Descriptor context-param—Specifies initialization parameters for the web application
Adding Listeners inThe Deployment Descriptor listener—Specifies the classes that listen for the application events
練習 • 寫一個 MyServletContextAttributeListener 實作 ServletContextAttributeListener • 把 ServletContext Attribute 的變動以 Log 印出 • 練習將屬性存放在 ServletContext 之中(Application Scope) • 做成一個 Visitor Counter • 『系統開機以來瀏覽人次=N』
Web Applications in a Distributed Environment • Distribute the applications across multiple server machines that are configured to work as a cluster • Advantages • Fail-over support • Load balancing
Web Applications in a Distributed Environment • Behavior of a ServletContext • Each web application has one and only one ServletContext instance on each JVM • ServletContext attributes set on one JVM are not visible on another JVM • Changes to the ServletContext in one JVM may not trigger a method call on a Listener in another JVM • ServletContext initialization parameters are available in all of the JVMs
Web Applications in a Distributed Environment • Behavior of an HttpSession • An HttpSession can reside on only one JVM at a time • A servlet container is not required to propagate HttpSessionEvents to different JVMs • Attributes of a session that implement the java.io.Serializable interface are migrated appropriately when the session migrates
Web Applications in a Distributed Environment • Behavior of an HttpSession • A container notifies all of the session attributes that implement the HttpSessionActivationListener interface when it migrates the session • A container may throw an IllegalArgumentException in the setAttribute() method of HttpSession if the attribute is not Serializable
SUMMARY The servlets of a web application share the application’s environment through the methods of the ServletContext object Listener interfaces are implemented in order to receive notifications of certain events in a web application A web application can be distributed across multiple servers to improve performance and reliability