Using STRUTS2 as a server framework, interacting with Android clients requires a request, response object.
There are two ways to get request and response in Struts2.
The first: static methods using Servletactioncontext
STRUTS2 uses the Servletactioncontext class to maintain servlet objects, Servletactioncontext uses threadlocal to maintain servlet objects of different threads. Therefore, it can be obtained using the Servletactioncontext class, which can be called non-injection (non-IOC) mode.
Public classLoginaction extends Actionsupport {Private StaticFinalLongSerialversionuid =1L; PrivateString account;//Account Number PrivateString pwd;//Password PublicString Login () throws exception{//GET RequestHttpServletRequest request=servletactioncontext.getrequest (); //Get ResponseHttpServletResponse response=Servletactioncontext.getresponse (); //Get SessionHttpSession Session=request.getsession (true); //put it in the session.Session.setattribute (" Account", account); //Get ContextServletContext context=Servletactioncontext.getservletcontext (); //Get upload folderFile uploadfile=NewFile (Context.getrealpath ("Upload")); //returning results to the clientPrintWriter writer=Response.getwriter (); Writer.print (" Account"); returnexecute (); }
The second type: using the relevant aware interface
STRUTS2 provides an aware interface that enables the action of the relevant aware interface to perceive the corresponding resource. When struts instantiates an action case, if it finds that it implements the corresponding aware interface, it injects the corresponding resources through the Aware interface method, which can be called the injection method (IOC mode).
Public classServletawareaction extends Actionsupport implements Servletrequestaware, Servletresponseaware, Se Ssionaware, Servletcontextaware {Private StaticFinalLongSerialversionuid =1L; PrivateServletContext Application;//servlet Context PrivateHttpServletRequest request;//Request Object PrivateHttpServletResponse response;//Response Object PrivateMap<string, object> session;//Session Object Public voidsetservletrequest (HttpServletRequest request) { This. request=request; } Public voidSetservletresponse (httpservletresponse response) { This. response=response; } Public voidSetsession (map<string, object>session) { This. session=session; } Public voidSetservletcontext (ServletContext application) { This. application=Application; } Publicstring Execute () {string upload=application.getrealpath ("Upload"); Request.getremoteaddr (); Response.getcontenttype (); Session.Get(" Account"); returnaction.success; }
The aware interface is actually an interceptor, and the interceptor code executes before the action is executed, setting the relevant Servlet object in
STRUTS2 GET request, response, interact with Android client (file passed to client)