转载自:http://blog.csdn.net/ranmudaofa/article/details/39623901
思路:网上找了好多,都说没有直接根据id获取session的方法,只能通过spring提供的HttpSessionListener监听器,监听session的创建和删除,然后把响应的操作记录到自己定义的map里,这样就可以从这个map里根据sessionid获取session了。
代码如下:
public
class
MySessionListener
implements
HttpSessionListener{
public
static
HashMap
mymap
=
new
HashMap();
public
static
synchronized
void
AddSession(HttpSession session) {
if
(session !=
null
) {
mymap
.put(session.getId(), session);
}
}
public
static
synchronized
void
DelSession(HttpSession session) {
if
(session !=
null
) {
mymap
.remove(session.getId());
}
}
public
static
synchronized
HttpSession getSession(String session_id) {
if
(session_id ==
null
)
return
null
;
return
(HttpSession)
mymap
.get(session_id);
}
@Override
public
void
sessionCreated(HttpSessionEvent httpsessionevent) {
MySessionContext. AddSession(httpsessionevent.getSession());
}
@Override
public
void
sessionDestroyed(HttpSessionEvent httpsessionevent) {
MySessionContext. DelSession(httpsessionevent.getSession());
}
}