This page (revision-7) was last changed on 15-Dec-2014 12:36 by Dieter Käppel

This page was created on 19-Nov-2012 13:14 by Dieter Käppel

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
7 15-Dec-2014 12:36 5 KB Dieter Käppel to previous
6 15-Dec-2014 12:36 4 KB Dieter Käppel to previous | to last
5 12-Mar-2013 12:59 4 KB Dieter Käppel to previous | to last
4 31-Dec-2012 14:54 4 KB Dieter Käppel to previous | to last
3 31-Dec-2012 14:52 2 KB Dieter Käppel to previous | to last
2 31-Dec-2012 14:44 1 KB Dieter Käppel to previous | to last
1 19-Nov-2012 13:14 2 KB Dieter Käppel to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 49 added 35 lines
!!!Session Events
In einem Servlet treten neben dem HttpServletRequest auch Events vom Typ HttpSessionEvent auf, wie zum Beispiel bei einem HttpSessionListener. Möchte man darin Spring-Operationen durchführen, braucht man einen ordentlich initialisierten Spring-Context inklusive den RequestAttributes.
Die Klasse EventRequestAttributes stellt diese zur Verfügung:
{{{
WebApplicationContext applicationContext =
WebApplicationContextUtils.getRequiredWebApplicationContext(event.getSession().getServletContext());
EventRequestAttributes request = new EventRequestAttributes(event);
try {
RequestContextHolder.setRequestAttributes(request);
} finally {
request.requestCompleted();
RequestContextHolder.resetRequestAttributes();
}
}}}
Die Klasse ContextSessionListener implementiert dies bereits für den HttpSessionListener, sodass sie bequem abgeleitet werden kann:
{{{
public class UserListener extends ContextSessionListener {
@Override
public void sessionCreated(HttpSessionEvent event, WebApplicationContext applicationContext) {
Authenticator authenticator = applicationContext.getBean(Authenticator.class);
authenticator.createSession(event.getSession());
}
@Override
public void sessionDestroyed(HttpSessionEvent event, WebApplicationContext applicationContext) {
Authenticator authenticator = applicationContext.getBean(Authenticator.class);
if (authenticator.getCurrentUser() != null)
authenticator.logout(event.getSession());
}
}
}}}