Servletcontext,actioncontext,servletactioncontext (Relationship and contact)

Source: Internet
Author: User
Tags locale


ServletContext ServletContext from his package information, it is the standard Java EE WebApplication class Library

Javax.servlet.ServletContext
ServletContext provides a standard servlet runtime environment, which is actually a way for some servlets and web container to communicate
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);
Note: A servlet that servletcontext corresponds to a namespace (for example, all Servlets under/struts) is shared by all servlets.

There is a 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 namespa Ce such as/catalog and possibly installed via a. war file.) The ServletContext is included in the ServletConfig object, and the ServletConfig object is usually read by the servlet or the init () method of the filter

Servletconfig.getservletcontext () Filterconfig.getservletcontext ()
Actioncontext originates from the Struts2 and Struts1 of the nature of the different. Struts1, when all *.do struts2 are processed by a servlet (servlet org.apache.struts.action.ActionServlet), a filter ( Org.apache.struts2.dispatcher.FilterDispatcher) handles all requests struts1 still belongs to the servlet category, Struts1 action is still the servlet. Struts2 action is already an ordinary Java bean, has jumped out of the servlet framework Actioncontext is to make up for strtus2 action out of the standard servlet framework caused by the Web environment lost contact with the flaw
Actioncontext's main role: to provide a Web environment context to address thread safety issues and to resolve some compatibility issues with other frameworks or containers (sitemesh,weblogic)

Analysis Actioncontext Source code

public class Actioncontext implements Serializable {
  Actioncontext instances in//////////threadlocal mode for thread safety under multithreading///////////////
     static ThreadLocal Actioncontext = new ThreadLocal ();          //sets The action context for the current thread.      public static void SetContext (Actioncontext context) {      & nbsp;  Actioncontext.set (context);     }     //returns the Actioncontext specific to the current thread.      public static Actioncontext GetContext () {         Return (Actioncontext) actioncontext.get ();     }
Defines a Map container that places "name/value pairs", which is the main function of 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 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 Actioncontext. The value can is looked up using the key.      public void put (String key, Object value) {         Context.put (key, value);     }
Placing various feature attributes into the map container/////////////////////                 //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"; Various action main properties: ActionName, actioninvocation (call action information), 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);     }
Various request requests contain content////////////////////    //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) {& nbsp;        put (PARAMETERS, PARAMETERS);     }             public void setsession (MAP <string, object> session) {         put (session, session);      }      public map<string, object> getsession () {                   return (map<string, object>) Get (SESSION);    &nbSp }           public void Setapplication (map<string, object> application) {         put (application, application);      }      public map<string, object> getapplication () {     & nbsp;   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) {            &nbsP 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 () {      & nbsp;  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 () {     & nbsp;   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 a initialized container for this request.");         }     }}

Servletactioncontext is actually actioncontext subclass, its function is born out of Actioncontext, to Actioncontext method to do a certain package, provide a more simple and intuitive method

public class Servletactioncontext extends Actioncontext implements Strutsstatics {
Servlet Context provides a number of static methods for manipulating Actioncontext, making it easier to get web objects
       //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);      }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.