面试--servlet如何获取session?(51)
来源:互联网 发布:net域名注册 编辑:程序博客网 时间:2024/06/05 19:05
The servlet can get a session object by calling the getSession method of the javax.servlet.http.HttpServletRequest interface. When the getSession method is invoked, the request object must somehow call the manager associated with the context. The manager either creates a new session object or returns the existing one. For the request object to be able to access the manager, it must have access to the context. To achieve this, in the invoke method of the SimpleWrapperValve class, you call the setContext method of the org.apache.catalina.Request interface, passing the Context. Remember that the SimpleWrapperValve calss’s invoke method calls the requested servlet’s service method. The invoke method of the SimpleWrapperValve class is given in following:
public void invoke(Request request, Response response, ValveContext valveContext)throws IOExveption, ServletException{ SimpleWrapper wrapper = (SimpleWrapper)getContainer(); ServletRequest req = request.getRequest(); ServletResponse sres = response.getResponse(); Servlet servlet = null; HttpServletRequest hreq = null; if(sreq instanceof HttpServletRequest) hreq = (HttpServletRequest)sreq; HttpServletResponse hres = null; if(sres instanceof HttpServletResponse) hres = (HttpServletResponse)sres; //pass the Context to the Request object so that //the Request object can call the Manager Context context =(Context)wrapper.getParent(); request.setContext(context); //Allocate a servlet instance to process this request try{ servlet = wrapper.allocate(); if(hres!=null && hreq!=null){ servlet.service(hreq,hres); }else{ servlet.service(sreq,sres); } }catch(ServletException e){} }
You have access to the wrapper ,therefore you can obtain the context by calling the getParent method of the Container interface. Note that the wrapper was added to a context. Once you have the context, you can then call the setContext method of the Request interface. Once you have the manager, obtaining a session object or creating a new one is straightforward.
- 面试--servlet如何获取session?(51)
- Servlet--获取Session对象
- 如何获取Session
- Servlet(五)servlet Session
- 在java中如何在非servlet的普通类中获取request、response、session
- Servlet Session(会话)
- servlet获取session值的纠结
- 【转载】servlet中创建与获取session
- 如何根据sessionID获取session
- struts2下如何获取session
- Servlet中如何捕获Session事件
- Servlet中如何捕获Session事件?
- Servlet中如何捕获Session事件
- Servlet中如何捕获Session事件?
- Servlet中如何捕获Session事件?
- Servlet中如何捕获Session事件
- Servlet中如何捕获Session事件
- Servlet中如何捕获Session事件?
- javascript——卡片切换
- js_for循环
- 面向对象——继承
- Ember 翻译——教程四:模型钩子
- 随笔
- 面试--servlet如何获取session?(51)
- tomcat7的性能优化
- 二分查找算法 php
- 用户名不在sudoers文件中,此事将被报告。
- AHOI2009维护序列--线段树lazy tag模板
- 浅谈游戏热更新
- elf文件格式解析
- First blog!
- MySQL数据库导出与导入及常见错误解决(转)