struts2访问Servlet API

struts2提供了两种方式访问Servlet API:

1.伪访问. 借助于ActionContext

以操作session为例:

ActionContext actCtx = ActionContext.getContext();
Map<String,Object> sess = actCtx.getSession();//该map模拟HTTP Session
sess.put("xxx",xxx);          

2. 真访问。借助于ServletActionContext

以添加cookie为例(添加cookie必须用httpServletResponse对象,所以Action必须与Servlet API耦合)

HttpServletResponse response = ServletActionContext.getResponse();
Cookie cookie = new Cookie("xx",xx);
response.addCookie(cookie);

ss

原文地址:https://www.cnblogs.com/ssyz1988/p/3492609.html