Java通过sessionId获取Session
2016-08-09 16:33
281 查看
Servlet2.1之后不支持SessionContext里面getSession(String id)方法。
但是,我们可以通过HttpSessionListener监听器和全局静态map自己实现一个SessionContext。
MySessionContext.java:
public class MySessionContext {
private 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);
}
}
MySessionListener.java:
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);
}
}
web.xml添加一个监听器:
<listener>
<listener-class>listener.MySessionListener</listener-class>
</listener>
根据sessionId获取Session对象:
String sessionId = request.getParameter("sessionId");
HttpSession session = MySessionContext.getSession(sessionId);
但是,我们可以通过HttpSessionListener监听器和全局静态map自己实现一个SessionContext。
MySessionContext.java:
public class MySessionContext {
private 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);
}
}
MySessionListener.java:
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);
}
}
web.xml添加一个监听器:
<listener>
<listener-class>listener.MySessionListener</listener-class>
</listener>
根据sessionId获取Session对象:
String sessionId = request.getParameter("sessionId");
HttpSession session = MySessionContext.getSession(sessionId);
相关文章推荐
- Java通过sessionId获取Session
- Java 通过 sessionId 获取 Session
- Java通过sessionId获取Session
- 1.Java通过sessionid获取session对象
- Java通过sessionId获取Session
- struts2 jsp 通过session获取登录用户id并把值加到流中
- Java根据sessionId获取Session对象
- Java根据sessionId获取Session对象
- Java中通过DWR获取session、request、response
- Java根据Session Id获取Session对象(转)
- Android 通过resource名称或者layout中定义的id name 去获取R.java中id,得到控件
- thinkphp 通过获取用户登录时的session_id来获取session里面的数据传到前台默认显示
- Java中session保存和获取验证码获取的id不一致问题
- Java根据sessionId获取Session对象
- JAVA程序通过后台登陆网站,获取Session,然后再POST Http Request添加数据到数据库的实例
- JavaWeb根据sessionId获取session对象
- 通过sessionId获取session
- Java根据sessionId获取Session对象
- jsp中插入java代码,并在当前页面获取值(也可在另一js种通过input标签id获取值)
- Java根据sessionId获取Session对象