最近和app 对接登录时,需要做session保持通讯。这里整理记录下
主要实现是:用户登录成功后返回sessionID 给app ,app 上需要用户登录后才能操作的,在每次请求的时候把sessionID 当成参数传过来。
web.xml代码:(主要是建立一个监听)
<listener>
<listener-class>com.ptpl.controller.ui.MySessionListener</listener-class>
</listener>
MySessionListener代码:
package com.ptpl.controller.ui;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
MySessionContext.AddSession(httpSessionEvent.getSession());
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
MySessionContext.DelSession(session);
}
}
MySessionContext代码:
package com.ptpl.controller.ui;
import java.util.HashMap;
import javax.servlet.http.HttpSession;
public class MySessionContext {
private static HashMap mymap = new HashMap();
public static synchron