将httpsession的实例传到DAO里面
解决方案 »
- 进公司做数据库开发、数据库编程都做什么工作,会涉及到哪些技能?
- HibernateTemplate update() 问题的解决
- apache+resin整合问题
- Struct ACTION报javax.servlet.ServletException 怎么改??
- 问一个编译EJB时的错误
- 小弟弟我是初学,关于方法的添加
- 急急急,5555~~~~ 用JAVA 怎么发传真啊
- 求struts+hibernate代码
- 请问各位高人在Struts1.2中如何实现对资源文件的支持?
- UnmarshalException问题。
- 為什麼加上<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%> 頁面不顯示??!!!!!!!!!!
- 正则表达式
你可以考虑先增加一个Filter过滤所有的请求,类似代码
public class MyFilter implements Filter{
public static ThreadLocal session = new ThreadLocal(); public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
session.set(((HttpServletRequest)req).getSession());
chain.doFilter(req, resp);
}
}然后你在DAO中直接MyFilter.session.get()就可以取出Session
ThreadLocal可以保证不会出现线程安全问题
新建一个Filter,就是我上述的代码。在web.xml中配置这个Filter过滤所有的请求,也就是/*
然后你在任何地方都可以MyFilter.session.get()来得到正确的Session了
public class ThreadSession { private static ThreadLocal _session = new ThreadLocal(); public static HttpSession getHttpSession() {
ThreadLocal threadlocal = _session;
synchronized (threadlocal) {
HashMap map = (HashMap) _session.get();
if (map != null)
return (HttpSession) map.get("httpSession");
}
return null;
}
public static void setHttpSession(HttpSession httpSession) {
synchronized (_session) {
HashMap map = (HashMap) _session.get();
if (map == null)
map = new HashMap();
map.put("httpSession", httpSession);
setObject(map);
}
}
}2.在Action入口处
ThreadSession.setHttpSession(request.getSession());3.在上记Action的出口之前所有地方,比如logic,DAO等都可以直接通过ThreadSession.getHttpSession()得到你保存过的对象.
只能用request了,再从request得到session.