1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package listeners; |
19 |
| |
20 |
| |
21 |
| import javax.servlet.ServletContext; |
22 |
| import javax.servlet.ServletContextEvent; |
23 |
| import javax.servlet.ServletContextListener; |
24 |
| import javax.servlet.http.HttpSessionAttributeListener; |
25 |
| import javax.servlet.http.HttpSessionBindingEvent; |
26 |
| import javax.servlet.http.HttpSessionEvent; |
27 |
| import javax.servlet.http.HttpSessionListener; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| public final class SessionListener |
41 |
| implements ServletContextListener, |
42 |
| HttpSessionAttributeListener, HttpSessionListener { |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| private ServletContext context = null; |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
0
| public void attributeAdded(HttpSessionBindingEvent event) {
|
63 |
| |
64 |
0
| log("attributeAdded('" + event.getSession().getId() + "', '" +
|
65 |
| event.getName() + "', '" + event.getValue() + "')"); |
66 |
| |
67 |
| } |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
0
| public void attributeRemoved(HttpSessionBindingEvent event) {
|
76 |
| |
77 |
0
| log("attributeRemoved('" + event.getSession().getId() + "', '" +
|
78 |
| event.getName() + "', '" + event.getValue() + "')"); |
79 |
| |
80 |
| } |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
0
| public void attributeReplaced(HttpSessionBindingEvent event) {
|
89 |
| |
90 |
0
| log("attributeReplaced('" + event.getSession().getId() + "', '" +
|
91 |
| event.getName() + "', '" + event.getValue() + "')"); |
92 |
| |
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
0
| public void contextDestroyed(ServletContextEvent event) {
|
102 |
| |
103 |
0
| log("contextDestroyed()");
|
104 |
0
| this.context = null;
|
105 |
| |
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
0
| public void contextInitialized(ServletContextEvent event) {
|
115 |
| |
116 |
0
| this.context = event.getServletContext();
|
117 |
0
| log("contextInitialized()");
|
118 |
| |
119 |
| } |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
0
| public void sessionCreated(HttpSessionEvent event) {
|
128 |
| |
129 |
0
| log("sessionCreated('" + event.getSession().getId() + "')");
|
130 |
| |
131 |
| } |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
0
| public void sessionDestroyed(HttpSessionEvent event) {
|
140 |
| |
141 |
0
| log("sessionDestroyed('" + event.getSession().getId() + "')");
|
142 |
| |
143 |
| } |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
0
| private void log(String message) {
|
155 |
| |
156 |
0
| if (context != null)
|
157 |
0
| context.log("SessionListener: " + message);
|
158 |
| else |
159 |
0
| System.out.println("SessionListener: " + message);
|
160 |
| |
161 |
| } |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
0
| private void log(String message, Throwable throwable) {
|
172 |
| |
173 |
0
| if (context != null)
|
174 |
0
| context.log("SessionListener: " + message, throwable);
|
175 |
| else { |
176 |
0
| System.out.println("SessionListener: " + message);
|
177 |
0
| throwable.printStackTrace(System.out);
|
178 |
| } |
179 |
| |
180 |
| } |
181 |
| |
182 |
| |
183 |
| } |