ServletContext

ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库
javax.servlet.ServletContext
ServletContext提供了标准的Servlet运行环境,其实就是一些servlet和web container进行通信的方法
public interface ServletContext { // Returns the URL prefix for the ServletContext.
public String getServletContextName(); //Returns the ServletContext for the uri.
public ServletContext getContext(String uri); //Returns the context-path for the web-application.
public String getContextPath(); //Returns the real file path for the given uri.
public String getRealPath(String uri); public RequestDispatcher getRequestDispatcher(String uri);
public RequestDispatcher getNamedDispatcher(String servletName);
public Object getAttribute(String name);
public Enumeration getAttributeNames();
public void setAttribute(String name, Object value);
public void removeAttribute(String name);
注意:一个ServletContext对应一个命名空间的servlet( 比如/struts下的所有servlet),是被所有servlet共享的.
There is one context per "web application" per Java Virtual Machine.
(A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.) ServletContext被包含在ServletConfig对象中,ServletConfig对象通常被servlet或filter的init()方法读取
ServletConfig.getServletContext()
filterconfig.getServletContext() ActionContext来源于Struts2 与 Struts1的本质不同.
struts1时,由一个servlet (servlet org.apache.struts.action.ActionServlet)处理所有的*.do
struts2时,由一个filter(org.apache.struts2.dispatcher.FilterDispatcher)处理所有的请求
struts1 仍旧属于servlet范畴,struts1 action 其本质仍是servlet.
struts2 action 已经是普通的Java bean了,已经跳出了servlet 框架
ActionContext就是为了弥补strtus2 action跳出标准servlet框架而造成的和WEB环境失去联系的缺陷 ActionContext的主要作用: 提供Web环境Context
解决线程安全问题
解决一些和其他框架或容器(siteMesh,webLogic)的兼容问题 分析ActionContext源码 public class ActionContext implements Serializable {
//////////ThreadLocal模式下的ActionContext实例,实现多线程下的线程安全/////////////// static ThreadLocal actionContext = new ThreadLocal(); //Sets the action context for the current thread.
public static void setContext(ActionContext context) {
actionContext.set(context);
}
//Returns the ActionContext specific to the current thread.
public static ActionContext getContext() {
return (ActionContext) actionContext.get();
}
///////////////定义放置"名/值对"的Map容器,这是ActionContext的主要功能/////////////// Map<String, Object> context; // constractor
// Creates a new ActionContext initialized with another context.
public ActionContext(Map<String, Object> context) {
this.context = context;
} public void setContextMap(Map<String, Object> contextMap) {
getContext().context = contextMap;
}
public Map<String, Object> getContextMap() {
return context;
} //Returns a value that is stored in the current ActionContext by doing a lookup using the value's key.
public Object get(String key) {
return context.get(key);
}
//Stores a value in the current ActionContext. The value can be looked up using the key.
public void put(String key, Object value) {
context.put(key, value);
}
///////////////////将各种功能属性放置入Map容器中///////////////////// //action name, Constant for the name of the action being executed.
public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name"; // ognl value stack
public static final String VALUE_STACK = ValueStack.VALUE_STACK; public static final String SESSION = "com.opensymphony.xwork2.ActionContext.session";
public static final String APPLICATION = "com.opensymphony.xwork2.ActionContext.application";
public static final String PARAMETERS = "com.opensymphony.xwork2.ActionContext.parameters";
public static final String LOCALE = "com.opensymphony.xwork2.ActionContext.locale";
public static final String TYPE_CONVERTER = "com.opensymphony.xwork2.ActionContext.typeConverter";
public static final String ACTION_INVOCATION = "com.opensymphony.xwork2.ActionContext.actionInvocation";
public static final String CONVERSION_ERRORS = "com.opensymphony.xwork2.ActionContext.conversionErrors";
public static final String CONTAINER = "com.opensymphony.xwork2.ActionContext.container"; ////// 各种Action主属性:ActionName, ActionInvocation(调用action的相关信息), ognl value stack/// //Gets the name of the current Action.
public String getName() {
return (String) get(ACTION_NAME);
}
//Sets the name of the current Action in the ActionContext.
public void setName(String name) {
put(ACTION_NAME, name);
} //Sets the action invocation (the execution state).
public void setActionInvocation(ActionInvocation actionInvocation) {
put(ACTION_INVOCATION, actionInvocation);
}
public ActionInvocation getActionInvocation() {
return (ActionInvocation) get(ACTION_INVOCATION);
} // Sets the OGNL value stack.
public void setValueStack(ValueStack stack) {
put(VALUE_STACK, stack);
}
//Gets the OGNL value stack.
public ValueStack getValueStack() {
return (ValueStack) get(VALUE_STACK);
} ////////////////各种 request请求包含的内容////////////////////
//Returns a Map of the HttpServletRequest parameters
public Map<String, Object> getParameters() {
return (Map<String, Object>) get(PARAMETERS);
}
public void setParameters(Map<String, Object> parameters) {
put(PARAMETERS, parameters);
} public void setSession(Map<String, Object> session) {
put(SESSION, session);
}
public Map<String, Object> getSession() {
return (Map<String, Object>) get(SESSION);
}
public void setApplication(Map<String, Object> application) {
put(APPLICATION, application);
}
public Map<String, Object> getApplication() {
return (Map<String, Object>) get(APPLICATION);
} public void setConversionErrors(Map<String, Object> conversionErrors) {
put(CONVERSION_ERRORS, conversionErrors);
}
public Map<String, Object> getConversionErrors() {
Map<String, Object> errors = (Map) get(CONVERSION_ERRORS); if (errors == null) {
errors = new HashMap<String, Object>();
setConversionErrors(errors);
}
return errors;
} //Sets the Locale for the current action.
public void setLocale(Locale locale) {
put(LOCALE, locale);
}
public Locale getLocale() {
Locale locale = (Locale) get(LOCALE); if (locale == null) {
locale = Locale.getDefault();
setLocale(locale);
} return locale;
}
public void setContainer(Container cont) {
put(CONTAINER, cont);
}
public Container getContainer() {
return (Container) get(CONTAINER);
} public <T> T getInstance(Class<T> type) {
Container cont = getContainer();
if (cont != null) {
return cont.getInstance(type);
} else {
throw new XWorkException("Cannot find an initialized container for this request.");
}
}
} ServletActionContext 其实是ActionContext的子类,其功能脱胎于ActionContext,对ActionContext的方法做了一定的包装,提供了更简便直观的方法 public class ServletActionContext extends ActionContext implements StrutsStatics {
/////////////////Servlet Context 提供了多种操作ActionContext的静态方法,使获得Web对象更方便 //HTTP servlet request
public static void setRequest(HttpServletRequest request) {
ActionContext.getContext().put(HTTP_REQUEST, request);
}
public static HttpServletRequest getRequest() {
return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST);
} //HTTP servlet response
public static void setResponse(HttpServletResponse response) {
ActionContext.getContext().put(HTTP_RESPONSE, response);
}
public static HttpServletResponse getResponse() {
return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE);
} //servlet context.
public static ServletContext getServletContext() {
return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT);
}
public static void setServletContext(ServletContext servletContext) {
ActionContext.getContext().put(SERVLET_CONTEXT, servletContext);
}

原文:http://blog.csdn.net/ocean1010/article/details/6160159

转:ServletContext,ActionContext,ServletActionContext的更多相关文章

  1. ServletContext ActionContext ServletActionContext

    1> ServletContext--------->SessionContext>RequestContext>PageContext 一个 WEB 运用程序只有一个 Ser ...

  2. java context 讲解

    在 java 中, 常见的 Context 有很多, 像: ServletContext, ActionContext, ServletActionContext, ApplicationContex ...

  3. 几个 Context 上下文的区别

    转自:http://www.blogjava.net/fancydeepin/archive/2013/03/31/java-ee-context.html 在 java 中, 常见的 Context ...

  4. spring context对象

    在 java 中, 常见的 Context 有很多, 像: ServletContext, ActionContext, ServletActionContext, ApplicationContex ...

  5. [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. 域对象的引用,ActionContext 和ServletActionContext类的使用

    ActionContext 获取 域引用的map ServletActionContext获取具体域对象 //域范围 ActionContext ac = ActionContext.getConte ...

  7. Struts2 在Action中获取request、session、servletContext的三种方法

    首页message.jsp: <body> ${requestScope.req }<br/> ${applicationScope.app }<br/> ${se ...

  8. Struts2初学 Struts2在Action获取内置对象request,session,application(即ServletContext)

    truts2在Action中如何访问request,session,application(即ServletContext)对象???? 方式一:与Servlet API解耦的方式      可以使用 ...

  9. ServletActionContext 源码

    /* * $Id: ServletActionContext.java 651946 2008-04-27 13:41:38Z apetrelli $ * * Licensed to the Apac ...

随机推荐

  1. 安装Linux 16.04 时,选择好分区后,进到选择地点的界面后,总是闪退,退到最原始的界面

    这两天装 Linux 系统,总是遇到一个很蛋疼的问题: 当你累死累活把分区什么的都设置好了之后,在输入了系统名字,开机密码那几项之后,再选择地点的时候(如:选择 "上海"),然后就 ...

  2. asp.net 设置网页过期

    /// <summary> /// 判断网页是否过期 /// </summary> /// <returns></returns> private bo ...

  3. python-print

    %s,%d就是占位符.还有%r 只是说明这样的对应:%s-->str();%r-->repr(),什么意思呢?就是说%s调用的是str()函数把对象转化为str类型,而%r是调用了repr ...

  4. 微信:JSSDK开发

    根据微信开发文档步骤如下: 1.先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. JS接口安全域名设置 mi.com(前面不用带www/http,域名必须备案过) 2.引 ...

  5. VR内容定制请找北京动软VR团队,长年承接VR/AR应用、游戏内容定制

    最近这一拔VR及AR浪潮得到业界的热捧,与2015年年底到2016年年初乐相.蚁视.睿悦.焰火工坊等VR创业公司,陆续发布融资的信息不无关系.业界也有统计数据称,约90%的VR投资案例,发生在2015 ...

  6. json在项目中的应用大总结

    一.摘要 刚开始接触json的时候,那时候还不太清楚json到底是个什么东西,然后就在项目中使用了它.因为没有搞明白json的本质,所以刚开始使用json的时候走了不少弯路.这次总结一些json的知识 ...

  7. putty远程连接ubuntu

      步骤一.在ubuntu系统中安装ssh,可使用如下的命令进行安装: sudo apt-get install openssh-server步骤二.为了保险起见,安装完成后重启一下ssh服务,命令如 ...

  8. css3基础、(弹性、响应式)布局注意点

    E1>E2选择父元素为E元素的所有E2元素(子类选择器) E1+E2选择元素为E1之后的所有E2元素(兄弟选择器) E[attr]只使用属性名,但没有确定任何属性值 E[attr="v ...

  9. XPath Checker和Firebug安装与使用

    一.XPath Checker和Firebug简介 XPath Checker和Firebug是写爬虫过程中提取数据的非常有用的插件工具,直接打开火狐浏览器的菜单就可以下载 二.XPath Check ...

  10. Could not find com.android.tools.build:gradle:1.3.0.

    * What went wrong:          A problem occurred configuring project ':TZYJ_Android'.> Could not re ...