1 / 24

The Servlet Container Model

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. 簡介.

haamid
Download Presentation

The Servlet Container Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Servlet Container Model 鄧姚文 joseph.deng@gmail.com

  2. 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

  3. 簡介 • 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

  4. 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

  5. Initialization Parameters for ServletContext

  6. How To Access ServletContext

  7. Adding and Listening to Scope Attributes • Type of scopes • Request: HttpServletRequest • Session: HttpSession • Application: ServletContext • Add/remove attributes • Listening to events related to attributes

  8. Adding and Removing Scope Attributes

  9. 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

  10. Listening to Request Attribute Events

  11. 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

  12. Listening to Context Attribute Events The ServletContextAttributeListener interface is used to receive notifications about the changes to the attribute list of a servlet context

  13. Servlet Life-Cycle Events and Listeners • javax.servlet.ServletContextListener • Allows a developer to know when a servlet context is initialized or destroyed

  14. 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*)>

  15. 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

  16. Adding Listeners inThe Deployment Descriptor context-param—Specifies initialization parameters for the web application

  17. Adding Listeners inThe Deployment Descriptor listener—Specifies the classes that listen for the application events

  18. 練習 • 寫一個 MyServletContextAttributeListener 實作 ServletContextAttributeListener • 把 ServletContext Attribute 的變動以 Log 印出 • 練習將屬性存放在 ServletContext 之中(Application Scope) • 做成一個 Visitor Counter • 『系統開機以來瀏覽人次=N』

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

More Related