今天写程序要在一个JAVABEAN里面用到SERVLET,我们用的是WEBWORK的框架,居然发现在BEAN里面可以直接使用WEBWORK提供的JSP的各种数据对象,方法如下:
- ActionContext context = ActionContext.getContext();
request 对象 HttpServletRequest request = (HttpServletRequest)context.get(HTTP_REQUEST);
- response对象 HttpServletRequest response= (HttpServletResponse)context.get(HTTP_RESPONSE);
- servletContext对象 ServletContext servletcontext= (ServletContext)context.get(
- servletDispather 对象 ServletDispather servletDispather= (ServletDispatcher)context.get(SERVLET_DISPATCHER);
- pageContext对象 PageContext pageContext = (PageContext)context.get(
- 我在程序中是这样用的
- ActionContext ac = ActionContext.getContext();
- ServletContext sct = (ServletContext)ac.get(ServletActionContext.SERVLET_CONTEXT);
- 用的时候是直接在一个JAVABEAN里面用的,居然挺管用,另附我用BEAN初始化APPLICATIONCONTEXT.XML的代码
- ApplicationContext act = WebApplicationContextUtils.getWebApplicationContext(sct);
- SessionFactory sessionFactory = (SessionFactory)act.getBean("sessionFactory");
- Session session = sessionFactory.openSession();
- ......