根据sessionid获取session的被Servlet2.1抛弃getsession方法的解决方案
2011-03-02 14:20
741 查看
最近一个项目中用到了getsession根据sessionid来获取session,但是怎么获取都为空,请教N多人,才发现在servlet的api中有如下:
不赞成的用法. Java Servlet API的版本 2.1中,还没有将之替换掉。该方法必须返回一个空值,且将会在未来的版本中被抛弃掉。
最新的解决办法是通过实现HttpSessionListener的sessionCreated和sessionDestroyed来实现
解决步骤:
1、LoginSessionListener:
LoginSessionListener方法实现了HttpSessionListener,并且重写sessionCreated和sessionDestroyed方法
web.xml中自动添加:
2、session的单例管理
3、这样我们就可以单例来获取session
参考URL:http://laolang.cn/back-end-develop/getsessionjava-servlet-api.html
HttpSession
HttpSessionContext.getSession(java.lang.String sessionId)
不赞成的用法. Java Servlet API的版本 2.1中,还没有将之替换掉。该方法必须返回一个空值,且将会在未来的版本中被抛弃掉。
最新的解决办法是通过实现HttpSessionListener的sessionCreated和sessionDestroyed来实现
解决步骤:
1、LoginSessionListener:
LoginSessionListener方法实现了HttpSessionListener,并且重写sessionCreated和sessionDestroyed方法
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class LoginSessionListener implements HttpSessionListener { // MySessionContext是实现session的读取和删除增加单例模式 private MySessionContext myc = MySessionContext.getInstance(); public void sessionCreated(HttpSessionEvent event) { myc.AddSession(event.getSession()); } public void sessionDestroyed(HttpSessionEvent event) { HttpSession session = event.getSession(); myc.DelSession(session); } }
web.xml中自动添加:
<listener> <listener-class>com.ejitec.util.LoginSessionListener</listener-class> </listener>
2、session的单例管理
@SuppressWarnings({ "rawtypes", "unchecked" }) public class MySessionContext { private static MySessionContext instance; private HashMap mymap; private MySessionContext() { mymap = new HashMap(); } public static MySessionContext getInstance() { if (instance == null) { instance = new MySessionContext(); } return instance; } public synchronized void AddSession(HttpSession session) { if (session != null) { mymap.put(session.getId(), session); } } public synchronized void DelSession(HttpSession session) { if (session != null) { mymap.remove(session.getId()); } } public synchronized HttpSession getSession(String session_id) { if (session_id == null) return null; return (HttpSession) mymap.get(session_id); } }
3、这样我们就可以单例来获取session
String sessionId = req.getParameter("jsessionid"); MySessionContext context = MySessionContext.getInstance(); HttpSession session = context.getSession(sessionId);
参考URL:http://laolang.cn/back-end-develop/getsessionjava-servlet-api.html
相关文章推荐
- 根据sessionid获取session的被Servlet2.1抛弃getsession方法的解决方案
- 根据sessionid获取session的被Servlet2.1抛弃getsession方法的解决方案
- EAS开发中根据单据id获取实体名的方法(bosType、表名等)
- Java根据Session Id获取Session对象(转)
- Java根据sessionId获取Session对象
- 如何根据sessionID获取session
- Java根据sessionId获取Session对象
- 获取servlet API的方法------ActionContext VS sessionAware
- 根据sessionId获取相应的session
- spring boot实现session 同步共享及 根据sessionid 获取相应的session
- 根据sessionId获取Session对象
- php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法
- 根据sessionId获取session对象
- WordPress根据页面名称获取页面id方法
- EASBOS根据id获取对象方法来进行过滤字段
- WordPress根据页面名称获取页面id方法
- JavaWeb根据sessionId获取session对象
- 07-session实现简易购物车2(获取sessionid方法SID)
- Java根据sessionId获取Session对象
- jsTree 点击节点事件及根据 ID 获取节点方法