1 / 11

Web Application Basic Scopes

Web Application Basic Scopes. Application Session Request Page. Sessions. … HttpSession session = request.getSession(); … session.getAttribute(String name) session.setAttribute(String name) session.removeAttribute(String name) Session.invalidate() Remember there are resources costs….

Download Presentation

Web Application Basic Scopes

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. Web Application Basic Scopes • Application • Session • Request • Page

  2. Sessions … HttpSession session = request.getSession(); … session.getAttribute(String name) session.setAttribute(String name) session.removeAttribute(String name) Session.invalidate() • Remember there are resources costs…

  3. Controlling Session Timeouts • Programmatically: • session.setMaxInactiveInterval(int seconds) • Declaratively (web.xml) <session-config> <session-timeout>180</session-timeout><!--minutes--> </session-config> • Negative value means the session will never expire (watch out!). Same result if 0 is set declaratively.

  4. Application-Wide Initialization Parameters • web.xml element: context-param <context-param> <param-name>support-email</param-name> <param-value>blackhole@mycompany.com</param-value> </context-param> • Read with the getInitParameter method of ServletContext (not ServletConfig which is used for servlet/jsp read of <init-param> element) • You may also use attributes for handling objects

  5. Listeners • A listener is an event handler that the server invokes when certain events occur (e.g. web application initialization/shutdown, session created/timed out…) • In design pattern terms – observer pattern: An observer (in this case the listener) is notified when an event occurs in the subject(server). • Typical uses: • Application-wide initialization routines • Managing dependencies between data stored in context or session attributes • Monitoring the running application (e.g. number of current sessions)

  6. Listeners – Cont. There are different kinds of listener, each corresponding to an interface and a group of events. Some of them are: • ServletContextListener • Web application initialized / shut down • ServletRequestListener • request handler starting / finishing • HttpSessionListener • session created / invalidated • ServletContextAttributeListener • context attribute added / removed / replaced • HttpSessionAttributeListener • session attribute added / removed / replaced

  7. Listeners – Cont. • To use a listener one simply implements the appropriate interface and registers the listener in the deployment descriptor. • As an example, the following listener monitors the current and maximum number of active sessions. • This class implements both HttpSessionListener and ServletContextListener such that it is notified when the application starts, to initialize the context attributes, and when a session is created or invalidated.

  8. Example: SessionMonitor (1/2) import javax.servlet.*; import javax.servlet.http.*; public class SessionMonitor implements HttpSessionListener, ServletContextListener { private int active = 0, max = 0; public void contextInitialized(ServletContextEvent sce) { store(sce.getServletContext()); } public void contextDestroyed(ServletContextEvent sce) {} public void sessionCreated(HttpSessionEvent se) { active++; if (active>max) max = active; store(se.getSession().getServletContext()); } Next Slide (Update Context Attributes)

  9. Example: SessionMonitor (2/2) public void sessionDestroyed(HttpSessionEvent se) { active--; store(se.getSession().getServletContext()); } private void store(ServletContext c) { c.setAttribute("sessions_active", new Integer(active)); c.setAttribute("sessions_max", new Integer(max)); } } Registration in web.xml: <listener> <listener-class>SessionMonitor</listener-class> <listener> Context Attributes Now that we have the class, what must we do?

  10. Filters • Code being executed before / after the servlet • Can intercept and redirect processing • security • auditing • Can modify requests and responses • data conversion (XSLT, gzip, ...) • specialized caching – all without changing the existing servlet code!

  11. Resources • An Introduction to XML and Web Technologies / Anders Møller and Michael I. Schwartzbach – course literature • Coreservlets.com

More Related