今天做了一个如何在struts的action获取servlet api的方法
由于struts是对请求和响应的再次封装,所以在struts2不能直接获取servlet api
//首先获取ActionContext对象
ActionContext ac = ActionContext.getContext();
//再通过ActionContext获得Session 或者Application的Map
Map<String,Object> session = ac.getSession();
Map<String,Object> application = ac.getApplication();
//对session 和application是通过put和get进行操作的
session.put("username", username);
application.put("counter", counter);
//这里是获取response对象 request也一样
HttpServletResponse response = ServletActionContext.getResponse();
//当然也可通过实现HttpServletResponseAware接口等获取responserequest对象
由于struts是对请求和响应的再次封装,所以在struts2不能直接获取servlet api
//首先获取ActionContext对象
ActionContext ac = ActionContext.getContext();
//再通过ActionContext获得Session 或者Application的Map
Map<String,Object> session = ac.getSession();
Map<String,Object> application = ac.getApplication();
//对session 和application是通过put和get进行操作的
session.put("username", username);
application.put("counter", counter);
//这里是获取response对象 request也一样
HttpServletResponse response = ServletActionContext.getResponse();
//当然也可通过实现HttpServletResponseAware接口等获取responserequest对象