1. ServletContext
  2.  
  3. ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库
  4. javax.servlet.ServletContext
  5. ServletContext提供了标准的Servlet运行环境,其实就是一些servletweb container进行通信的方法
  6. public interface ServletContext {
  7.  
  8. // Returns the URL prefix for the ServletContext.
  9. public String getServletContextName();
  10.  
  11. //Returns the ServletContext for the uri.
  12. public ServletContext getContext(String uri);
  13.  
  14. //Returns the context-path for the web-application.
  15. public String getContextPath();
  16.  
  17. //Returns the real file path for the given uri.
  18. public String getRealPath(String uri);
  19.  
  20. public RequestDispatcher getRequestDispatcher(String uri);
  21. public RequestDispatcher getNamedDispatcher(String servletName);
  22. public Object getAttribute(String name);
  23. public Enumeration getAttributeNames();
  24. public void setAttribute(String name, Object value);
  25. public void removeAttribute(String name);
  26. 注意:一个ServletContext对应一个命名空间的servlet( 比如/struts下的所有servlet),是被所有servlet共享的.
  27. There is one context per "web application" per Java Virtual Machine.
  28. (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.)
  29.  
  30. ServletContext被包含在ServletConfig对象中,ServletConfig对象通常被servlet或filter的init()方法读取
  31. ServletConfig.getServletContext()
  32. filterconfig.getServletContext()
  33.  
  34. ActionContext来源于Struts2 与 Struts1的本质不同.
  35. struts1时,由一个servlet (servlet org.apache.struts.action.ActionServlet)处理所有的*.do
  36. struts2时,由一个filter(org.apache.struts2.dispatcher.FilterDispatcher)处理所有的请求
  37. struts1 仍旧属于servlet范畴,struts1 action 其本质仍是servlet.
  38. struts2 action 已经是普通的Java bean了,已经跳出了servlet 框架
  39. ActionContext就是为了弥补strtus2 action跳出标准servlet框架而造成的和WEB环境失去联系的缺陷
  40.  
  41. ActionContext的主要作用:
  42.  
  43. 提供Web环境Context
  44. 解决线程安全问题
  45. 解决一些和其他框架或容器(siteMesh,webLogic)的兼容问题
  46.  
  47. 分析ActionContext源码
  48.  
  49. public class ActionContext implements Serializable {
  50. //////////ThreadLocal模式下的ActionContext实例,实现多线程下的线程安全///////////////
  51.  
  52. static ThreadLocal actionContext = new ThreadLocal();
  53.  
  54. //Sets the action context for the current thread.
  55. public static void setContext(ActionContext context) {
  56. actionContext.set(context);
  57. }
  58. //Returns the ActionContext specific to the current thread.
  59. public static ActionContext getContext() {
  60. return (ActionContext) actionContext.get();
  61. }
  62. ///////////////定义放置"名/值对"的Map容器,这是ActionContext的主要功能///////////////
  63.  
  64. Map<String, Object> context;
  65.  
  66. // constractor
  67. // Creates a new ActionContext initialized with another context.
  68. public ActionContext(Map<String, Object> context) {
  69. this.context = context;
  70. }
  71.  
  72. public void setContextMap(Map<String, Object> contextMap) {
  73. getContext().context = contextMap;
  74. }
  75. public Map<String, Object> getContextMap() {
  76. return context;
  77. }
  78.  
  79. //Returns a value that is stored in the current ActionContext by doing a lookup using the value's key.
  80. public Object get(String key) {
  81. return context.get(key);
  82. }
  83. //Stores a value in the current ActionContext. The value can be looked up using the key.
  84. public void put(String key, Object value) {
  85. context.put(key, value);
  86. }
  87. ///////////////////将各种功能属性放置入Map容器中/////////////////////
  88.  
  89. //action name, Constant for the name of the action being executed.
  90. public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";
  91.  
  92. // ognl value stack
  93. public static final String VALUE_STACK = ValueStack.VALUE_STACK;
  94.  
  95. public static final String SESSION = "com.opensymphony.xwork2.ActionContext.session";
  96. public static final String APPLICATION = "com.opensymphony.xwork2.ActionContext.application";
  97. public static final String PARAMETERS = "com.opensymphony.xwork2.ActionContext.parameters";
  98. public static final String LOCALE = "com.opensymphony.xwork2.ActionContext.locale";
  99. public static final String TYPE_CONVERTER = "com.opensymphony.xwork2.ActionContext.typeConverter";
  100. public static final String ACTION_INVOCATION = "com.opensymphony.xwork2.ActionContext.actionInvocation";
  101. public static final String CONVERSION_ERRORS = "com.opensymphony.xwork2.ActionContext.conversionErrors";
  102. public static final String CONTAINER = "com.opensymphony.xwork2.ActionContext.container";
  103.  
  104. ////// 各种Action主属性:ActionName, ActionInvocation(调用action的相关信息), ognl value stack///
  105.  
  106. //Gets the name of the current Action.
  107. public String getName() {
  108. return (String) get(ACTION_NAME);
  109. }
  110. //Sets the name of the current Action in the ActionContext.
  111. public void setName(String name) {
  112. put(ACTION_NAME, name);
  113. }
  114.  
  115. //Sets the action invocation (the execution state).
  116. public void setActionInvocation(ActionInvocation actionInvocation) {
  117. put(ACTION_INVOCATION, actionInvocation);
  118. }
  119. public ActionInvocation getActionInvocation() {
  120. return (ActionInvocation) get(ACTION_INVOCATION);
  121. }
  122.  
  123. // Sets the OGNL value stack.
  124. public void setValueStack(ValueStack stack) {
  125. put(VALUE_STACK, stack);
  126. }
  127. //Gets the OGNL value stack.
  128. public ValueStack getValueStack() {
  129. return (ValueStack) get(VALUE_STACK);
  130. }
  131.  
  132. ////////////////各种 request请求包含的内容////////////////////
  133. //Returns a Map of the HttpServletRequest parameters
  134. public Map<String, Object> getParameters() {
  135. return (Map<String, Object>) get(PARAMETERS);
  136. }
  137. public void setParameters(Map<String, Object> parameters) {
  138. put(PARAMETERS, parameters);
  139. }
  140.  
  141. public void setSession(Map<String, Object> session) {
  142. put(SESSION, session);
  143. }
  144. public Map<String, Object> getSession() {
  145. return (Map<String, Object>) get(SESSION);
  146. }
  147. public void setApplication(Map<String, Object> application) {
  148. put(APPLICATION, application);
  149. }
  150. public Map<String, Object> getApplication() {
  151. return (Map<String, Object>) get(APPLICATION);
  152. }
  153.  
  154. public void setConversionErrors(Map<String, Object> conversionErrors) {
  155. put(CONVERSION_ERRORS, conversionErrors);
  156. }
  157. public Map<String, Object> getConversionErrors() {
  158. Map<String, Object> errors = (Map) get(CONVERSION_ERRORS);
  159.  
  160. if (errors == null) {
  161. errors = new HashMap<String, Object>();
  162. setConversionErrors(errors);
  163. }
  164. return errors;
  165. }
  166.  
  167. //Sets the Locale for the current action.
  168. public void setLocale(Locale locale) {
  169. put(LOCALE, locale);
  170. }
  171. public Locale getLocale() {
  172. Locale locale = (Locale) get(LOCALE);
  173.  
  174. if (locale == null) {
  175. locale = Locale.getDefault();
  176. setLocale(locale);
  177. }
  178.  
  179. return locale;
  180. }
  181. public void setContainer(Container cont) {
  182. put(CONTAINER, cont);
  183. }
  184. public Container getContainer() {
  185. return (Container) get(CONTAINER);
  186. }
  187.  
  188. public <T> T getInstance(Class<T> type) {
  189. Container cont = getContainer();
  190. if (cont != null) {
  191. return cont.getInstance(type);
  192. } else {
  193. throw new XWorkException("Cannot find an initialized container for this request.");
  194. }
  195. }
  196. }
  197.  
  198. ServletActionContext 其实是ActionContext的子类,其功能脱胎于ActionContext,对ActionContext的方法做了一定的包装,提供了更简便直观的方法
  199.  
  200. public class ServletActionContext extends ActionContext implements StrutsStatics {
  201. /////////////////Servlet Context 提供了多种操作ActionContext的静态方法,使获得Web对象更方便
  202.  
  203. //HTTP servlet request
  204. public static void setRequest(HttpServletRequest request) {
  205. ActionContext.getContext().put(HTTP_REQUEST, request);
  206. }
  207. public static HttpServletRequest getRequest() {
  208. return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST);
  209. }
  210.  
  211. //HTTP servlet response
  212. public static void setResponse(HttpServletResponse response) {
  213. ActionContext.getContext().put(HTTP_RESPONSE, response);
  214. }
  215. public static HttpServletResponse getResponse() {
  216. return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE);
  217. }
  218.  
  219. //servlet context.
  220. public static ServletContext getServletContext() {
  221. return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT);
  222. }
  223. public static void setServletContext(ServletContext servletContext) {
  224. ActionContext.getContext().put(SERVLET_CONTEXT, servletContext);
  225. }

原文: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. [llvm] Call the LLVM Jit from c program

    stackoverflow: http://stackoverflow.com/questions/1838304/call-the-llvm-jit-from-c-program Another t ...

  2. 【OPENGL】第二篇 HELLO OPENGL(续)

    上一次我们在这里分析了OpenGL的例子,但是最后还少分析最重要的部分:着色器相关的代码.因此这一次作为前一篇文章的续集. 上一篇文章的地址 http://www.cnblogs.com/MyGame ...

  3. zendstuido10 配置spket插件

    必备:Zend Studio.Spket Plugin.sdk.jsb3.百度 安装过程中出现了两种错误,导致最后安装spket时报错,一种是提示“The file "F:\study\to ...

  4. 中文字符串转换为十六进制Unicode编码字符串

    package my.unicode; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Uni ...

  5. AngularJs之$scope对象(作用域)

      一.作用域 AngularJs中的$scope对象是模板的域模型,也称为作用域实例.通过为其属性赋值,可以传递数据给模板渲染. 每个$scope都是Scope类的实例,Scope类有很多方法,用于 ...

  6. Troubleshooting a node by using the netapp SP

    Troubleshooting a node by using the SP When you encounter a problem with a node, you can use the SP ...

  7. git如何设置账号密码

    查看已设配置 git config --list 修改GIT全局用户名 git config --global user.name [username] 修改GIT全局邮箱 git config -- ...

  8. 现代DOJO(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/modern_dojo/index.html 你可能已经不用doio一段时间了,或者你一直想保持 ...

  9. BaiduTemplate模板引擎使用示例附源码

    1.新建项目,asp.net 空Web应用程序 添加data,js,styles,templates文件夹,添加baiduTemplate.js,jquery.js,bootstrap.css 2.添 ...

  10. git服务器新增仓库

    在已有的git库中搭建新的库,并将本地的git仓库,上传到服务器的git库中,从而开始一个新的项目. 首先是在本地操作: 在本地新建文件夹spider,进入到spider中:如下