在building block里使用context后必须把其释放!!代码如下:
- ContextManager contextManager;
- contextManager = BbServiceManager.getContextManager();
- Context ctx = contextManager.setContext(request);
- .....
- contextManager.releaseContext();
那么在每个使用context的servlet里都调用这样几行代码就重复劳动了,应该把这几行代码写在servlet filter里:
//执行servlet之前运行的 代码
ContextManager contextManager;
contextManager = BbServiceManager.getContextManager();
contextManager.setContext((HttpServletRequest) request);
//进入下一个filter, if it is the last filter, access servlet
chain.doFilter(request, response);
//下面这行是在执行完servlet之后才运行的
contextManager.releaseContext();
那么在servlet里有2种方法来获取bb context
方法一:
Context bbContext = (Context) request.getAttribute("bbContext");
方法二:
ContextManager ctxMgr = (ContextManager) BbServiceManager.lookupService(ContextManager.class);
Context bbContext = ctxMgr.getContext();
servlet filter:http://zhangjunhd.blog.51cto.com/113473/20629