Ext.: servletcontext,actioncontext,servletactioncontext

Source: Internet
Author: User

Servletcontextservletcontext from his package information can be seen that it is the standard Java EE The WebApplication class Library Javax.servlet.ServletContextServletContext provides a standard servlet runtime environment, which is actually a way for some servlets and web container to communicate  Public InterfaceServletContext {//Returns the URL prefix for the ServletContext.  PublicString getservletcontextname ();//Returns The ServletContext for the URI.  PublicServletContext getcontext (String URI); //Returns The Context-path for the web-application.  PublicString Getcontextpath (); //Returns The real file path for the given URI.  Publicstring Getrealpath (string uri);  Publicrequestdispatcher getrequestdispatcher (String uri); Publicrequestdispatcher getnameddispatcher (String servletname); PublicObject getattribute (String name); Publicenumeration Getattributenames (); Public voidSetAttribute (String name, Object value); Public voidremoveattribute (String name); Note: A servletcontext that corresponds to a namespace, such as/all Servlets under Struts), are shared by all servlets. 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.)The ServletContext is included in the ServletConfig object, and the ServletConfig object is usually used by the servlet or the filter's init () Method reads Servletconfig.getservletcontext () Filterconfig.getservletcontext () Actioncontext from Struts2 and The nature of Struts1 is different. Struts1, a servlet (servlet org.apache.struts.action.ActionServlet) handles all*. DoStruts2, a filter (Org.apache.struts2.dispatcher.FilterDispatcher) handles all requests struts1 still belong to the servlet category, Struts1 Action is still the essence of servlet.struts2 action is already a common Java bean, has jumped out of the servlet framework Actioncontext is to make up for STRTUS2 Action jumps out of the standard servlet framework and loses contact with the Web environment the main role of the Actioncontext: to provide a Web environment context to solve some of the thread safety issues and other frameworks or containers (sitemesh,weblogic) The compatibility problem analysis Actioncontext source code Public classActioncontextImplementsSerializable {//////////Actioncontext instances in threadlocal mode for thread safety under multi-threading///////////////     StaticThreadLocal Actioncontext =NewThreadLocal (); //sets the action context for the current thread.      Public Static voidSetContext (Actioncontext context) {Actioncontext.set (context); }     //Returns the Actioncontext specific to the current thread.      Public StaticActioncontext GetContext () {return(Actioncontext) actioncontext.get (); } ///////////////define 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.      PublicActioncontext (map<string, object>context) {          This. Context =context; }           Public voidSetcontextmap (map<string, object>Contextmap) {GetContext (). Context=Contextmap; }      PublicMap<string, object>Getcontextmap () {returncontext; }//Returns A value is stored in the current actioncontext by doing a lookup using the value ' s key.      PublicObject get (String key) {returnContext.get (key); } //Stores A value in the current actioncontext. The value can is looked up using the key.      Public voidput (String key, Object value) {context.put (key, value); }///////////////////put various feature attributes into the map container/////////////////////                 //action name, Constant for the name of the action being executed.      Public Static FinalString action_name = "Com.opensymphony.xwork2.ActionContext.name"; //OGNL Value Stack      Public Static FinalString Value_stack =Valuestack.value_stack;  Public Static FinalString SESSION = "Com.opensymphony.xwork2.ActionContext.session";  Public Static FinalString application = "Com.opensymphony.xwork2.ActionContext.application";  Public Static FinalString PARAMETERS = "Com.opensymphony.xwork2.ActionContext.parameters";  Public Static FinalString LOCALE = "Com.opensymphony.xwork2.ActionContext.locale";  Public Static FinalString type_converter = "Com.opensymphony.xwork2.ActionContext.typeConverter";  Public Static FinalString action_invocation = "Com.opensymphony.xwork2.ActionContext.actionInvocation";  Public Static FinalString conversion_errors = "Com.opensymphony.xwork2.ActionContext.conversionErrors";  Public Static FinalString CONTAINER = "Com.opensymphony.xwork2.ActionContext.container"; //////various action main properties: ActionName, actioninvocation (call the relevant information for action), OGNL value Stack///                  //Gets the name of the current Action.      PublicString GetName () {return(String) get (Action_name); }     //Sets the name of the current Action in the actioncontext.      Public voidsetName (String name) {put (action_name, name); }          //sets the action invocation (the execution state).      Public voidsetactioninvocation (actioninvocation actioninvocation) {put (action_invocation, actioninvocation); }      Publicactioninvocation getactioninvocation () {return(actioninvocation) get (action_invocation); }      //sets the OGNL value stack.      Public voidsetvaluestack (Valuestack stack) {put (value_stack, stack); }     //Gets the OGNL value stack.      Publicvaluestack Getvaluestack () {return(Valuestack) get (Value_stack); }////////////////what the various request requests contain////////////////////     //Returns A Map of the httpservletrequest parameters      PublicMap<string, object>getparameters () {return(Map<string, object>) get (PARAMETERS); }      Public voidSetparameters (map<string, object>parameters)     {Put (PARAMETERS, PARAMETERS); }              Public voidSetsession (map<string, object>session)     {Put (session, session); }      PublicMap<string, object>getsession () {return(Map<string, object>) get (SESSION); }            Public voidSetapplication (map<string, object>application)     {Put (application, application); }      PublicMap<string, object>getapplication () {return(Map<string, object>) get (application); }       Public voidSetconversionerrors (map<string, object>conversionerrors)     {Put (conversion_errors, conversionerrors); }      PublicMap<string, object>getconversionerrors () {Map<string, object> errors =(MAP) get (conversion_errors); if(Errors = =NULL) {Errors=NewHashmap<string, object>();         Setconversionerrors (Errors); }         returnerrors; }      //Sets the Locale for the current action.      Public voidsetLocale (locale locale) {put (locale, locale); }      Publiclocale GetLocale () {locale locale=(locale) get (locale); if(Locale = =NULL) {locale=Locale.getdefault ();         SetLocale (locale); }          returnlocale; }      Public voidSetcontainer (Container cont) {put (Container, cont); }      PublicContainer GetContainer () {return(Container) get (Container); }          Public<T> T getinstance (class<t>type) {Container cont=GetContainer (); if(cont! =NULL) {             returncont.getinstance (type); } Else {             Throw NewXworkexception ("Cannot find a initialized container for this request.")); }}}servletactioncontext is actually a subclass of Actioncontext, its function is born out of Actioncontext, the Actioncontext method to do a certain packaging, provides a more convenient and intuitive method Public classServletactioncontextextendsActioncontextImplementsStrutsstatics {/////////////////servlet Context provides a number of static methods for manipulating Actioncontext, making it easier to get web objects//HTTP servlet Request       Public Static voidsetrequest (HttpServletRequest request) {Actioncontext.getcontext (). Put (http_request, request); }        Public Statichttpservletrequest getrequest () {return(HttpServletRequest) Actioncontext.getcontext (). get (Http_request); }       //HTTP servlet Response       Public Static voidSetresponse (httpservletresponse response) {Actioncontext.getcontext (). Put (http_response, response); }        Public StaticHttpServletResponse GetResponse () {return(HttpServletResponse) Actioncontext.getcontext (). get (Http_response); }       //servlet context.       Public StaticServletContext Getservletcontext () {return(ServletContext) Actioncontext.getcontext (). get (Servlet_context); }        Public Static voidSetservletcontext (ServletContext servletcontext) {Actioncontext.getcontext (). Put (Servlet_context, ServletCo      ntext); }

Original: http://blog.csdn.net/ocean1010/article/details/6160159

Ext.: servletcontext,actioncontext,servletactioncontext

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.