1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package listeners;
18
19
20 import javax.servlet.ServletContext;
21 import javax.servlet.ServletContextEvent;
22 import javax.servlet.ServletContextListener;
23 import javax.servlet.http.HttpSessionAttributeListener;
24 import javax.servlet.http.HttpSessionBindingEvent;
25 import javax.servlet.http.HttpSessionEvent;
26 import javax.servlet.http.HttpSessionListener;
27
28
29 /**
30 * Example listener for context-related application events, which were
31 * introduced in the 2.3 version of the Servlet API. This listener
32 * merely documents the occurrence of such events in the application log
33 * associated with our servlet context.
34 *
35 * @author Craig R. McClanahan
36 * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
37 */
38
39 public final class SessionListener
40 implements ServletContextListener,
41 HttpSessionAttributeListener, HttpSessionListener {
42
43
44
45
46
47 /**
48 * The servlet context with which we are associated.
49 */
50 private ServletContext context = null;
51
52
53
54
55
56 /**
57 * Record the fact that a servlet context attribute was added.
58 *
59 * @param event The session attribute event
60 */
61 public void attributeAdded(HttpSessionBindingEvent event) {
62
63 log("attributeAdded('" + event.getSession().getId() + "', '" +
64 event.getName() + "', '" + event.getValue() + "')");
65
66 }
67
68
69 /**
70 * Record the fact that a servlet context attribute was removed.
71 *
72 * @param event The session attribute event
73 */
74 public void attributeRemoved(HttpSessionBindingEvent event) {
75
76 log("attributeRemoved('" + event.getSession().getId() + "', '" +
77 event.getName() + "', '" + event.getValue() + "')");
78
79 }
80
81
82 /**
83 * Record the fact that a servlet context attribute was replaced.
84 *
85 * @param event The session attribute event
86 */
87 public void attributeReplaced(HttpSessionBindingEvent event) {
88
89 log("attributeReplaced('" + event.getSession().getId() + "', '" +
90 event.getName() + "', '" + event.getValue() + "')");
91
92 }
93
94
95 /**
96 * Record the fact that this web application has been destroyed.
97 *
98 * @param event The servlet context event
99 */
100 public void contextDestroyed(ServletContextEvent event) {
101
102 log("contextDestroyed()");
103 this.context = null;
104
105 }
106
107
108 /**
109 * Record the fact that this web application has been initialized.
110 *
111 * @param event The servlet context event
112 */
113 public void contextInitialized(ServletContextEvent event) {
114
115 this.context = event.getServletContext();
116 log("contextInitialized()");
117
118 }
119
120
121 /**
122 * Record the fact that a session has been created.
123 *
124 * @param event The session event
125 */
126 public void sessionCreated(HttpSessionEvent event) {
127
128 log("sessionCreated('" + event.getSession().getId() + "')");
129
130 }
131
132
133 /**
134 * Record the fact that a session has been destroyed.
135 *
136 * @param event The session event
137 */
138 public void sessionDestroyed(HttpSessionEvent event) {
139
140 log("sessionDestroyed('" + event.getSession().getId() + "')");
141
142 }
143
144
145
146
147
148 /**
149 * Log a message to the servlet context application log.
150 *
151 * @param message Message to be logged
152 */
153 private void log(String message) {
154
155 if (context != null)
156 context.log("SessionListener: " + message);
157 else
158 System.out.println("SessionListener: " + message);
159
160 }
161
162
163 /**
164 * Log a message and associated exception to the servlet context
165 * application log.
166 *
167 * @param message Message to be logged
168 * @param throwable Exception to be logged
169 */
170 private void log(String message, Throwable throwable) {
171
172 if (context != null)
173 context.log("SessionListener: " + message, throwable);
174 else {
175 System.out.println("SessionListener: " + message);
176 throwable.printStackTrace(System.out);
177 }
178
179 }
180
181
182 }