在Struts2里使用HttpServletRequest、HttpSession、ServletContext

在Struts2里使用HttpServletRequest、HttpSession、ServletContext


com.opensymphony.xwork2.ActionContext类
ActionContext是action执行的上下文,在ActionContext中保存了action执行所需的一组对象,包括parameters、request、session、application和locale等。
ActionContext类定义如下方法用于获取以上三个对象:
  public Object get(Object key)获取根据key参数的对象
  public Object get("request")获取封装了HttpServletRequest的Object对象,自己强制转换为Map对象
  public Map getSession()获取封装了HttpSession的Map对象
  public Map getApplication()获取封装了ServletContext的Map对象

例如:
在Session里面保存和获取验证码

ActionContext.getContext().getSession().put("sessioncode",code) ;
ActionContext.getContext().getSession().get("sessioncode") ;

你可能感兴趣的:(struts2)