ServletContext,ActionContext,ServletActionContext

碼字猴code_monkey發表於2014-08-21

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);
      }

相關文章