ActionContext 获取 域引用的map
ServletActionContext获取具体域对象
//域范围
ActionContext ac = ActionContext.getContext();
Map<String,Object> applicationMap = ac.getApplication();//这个就是ServletContext对象中维护的那个Map
applicationMap.put("p", "application_p");// ServletContext.setAttribute(key,object); Map<String,Object> sessionMap = ac.getSession();//这个就是HttpSession对象中维护的那个Map
sessionMap.put("p", "session_p");// HttpSession.setAttribute(key,object); ac.put("p", "request_p");//相当于ServletRequest.setAttribute(key,obj);
//获取Servlet中的常用对象的引用 //ServletContextAware接口 public class WebObjectAction extends ActionSupport implements Serializable,ServletContextAware,ServletRequestAware,ServletResponseAware {
private ServletContext context;
private HttpServletRequest request;
private HttpServletResponse response;
//方式一
public String execute1(){
ServletContext sc = ServletActionContext.getServletContext();
System.out.println(sc);
ServletRequest request = ServletActionContext.getRequest();
System.out.println(request);
return SUCCESS;
}
//方式二
public String execute2(){
System.out.println(context);
System.out.println(request);
System.out.println(response);
return SUCCESS;
}
public void setServletContext(ServletContext context) {//如果动作类实现了ServletContextAware接口,就会自动调用该方法
this.context = context;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
} }