Previously Servlet doget (Httpservletrequest,httpservletresponse), the Doget method could not be tested individually, and the request and response objects must be constructed to test
The Execute method of the Struts2 action is very easy to test independently, and this method does not rely on the servlet API
Servlet Common API objects: HttpServletRequest, HttpSession, ServletContext (three server-side data ranges), HttpServletResponse
1. Using Actioncontext with servlet API decoupling operation
1) Create Instance Actioncontext.getcontext ()
2) Get request parameter Actioncontext.getparameters () return map<string,string[]> representative request parameter Map equivalent to Request.getparametermap
3) Save a data actioncontext.put (Key,value) to the request; Equivalent to Request.setattribute ()
4) A Data actioncontext.get (key) reading the request range is equivalent to Request.getattribute ()
5) Read Session property Actioncontext.getsession () returns map Actioncontext.getsession (). Get (key) is equivalent to Session.getattribute ();
Actioncontext.getsession (). put (Key,value) equivalent to Session.setattribute ()
6) Read and write Servletcontexnt properties actioncontext.getapplication return to Map actioncontext.getapplication (). Get (Key) Equivalent to Servletcontext.getattribute
Actioncontext.getapplication (). put (Key,value) level in Servletcontext.setattribute
* The Servlet object was manipulated using Actioncontext, but the servlet API was not used
* For example: destroying session objects, adding cookies cannot be done by Actioncontext
2. Direct access to SERVLETAPI (Scheme one interface injection)
Servletcontextaware
void Setservletcontext (Javax.servlet.ServletContext context)
Servletrequestaware
void Setservletrequest (Javax.servlet.http.HttpServletRequest request)
Servletresponseaware
void Setservletresponse (Javax.servlet.http.HttpServletResponse response)
3. Direct access to the servlet API (scenario two: Servletactioncontext)
Static PageContext Getpagecontext ()
Static HttpServletRequest Getrequest ()
Static HttpServletResponse GetResponse ()
Static ServletContext Getservletcontext ()
Java EE----Struts2 Action access servlet API