STRUTS2 GET request, response, interact with Android client (file passed to client)

Source: Internet
Author: User

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)

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.