Struts2访问servlet的三种方式

第一种方式:使用ActionContext类实现

  1. //获取对象
  2. ActionContext context = ActionContext.getContext();
  3. //获取页面提交数据
  4. Map<String, Object> parameters = context.getParameters();
  5. //操作域对象相关的方法
  6. context.put(String,Object);//相当于HttpServletRequest的setAttribute方法
  7. context.getApplication();//返回一个Map对象,存取ServletContext属性
  8. context.getSession();//返回一个Map对象,存取HttpSession属性

第二种方式:在action里面实现不同的接口,得到servlet里面不同的对象

  1. ServletRequestAware
  2. ServletContextAware
  3. ServletResponseAware

第三种方式:使用ServletActionContext类

  1. HttpServletRequest request = ServletActionContext.getRequest();
  2. HttpServletResponse response = ServletActionContext.getResponse();





posted @ 2016-01-24 22:55  ty850454  阅读(182)  评论(0)    收藏  举报