STRUTS2 Study Note VI: STRUTS2 's action to access Servletapi several ways

Source: Internet
Author: User

Method One: Access Serlvetapi through Actioncontext, this way there is no intrusive action class part of the code
ImportCom.opensymphony.xwork2.ActionContext;  PublicString Execute ()throwsException {if("admin". Equals (User.getusername ()) && "Admin". Equals (User.getpassword ())) {            //Request for a map method//Actioncontext.getcontext (). Put ("msg", "Login Successful"); //get the session of Map mode//Actioncontext.getcontext (). GetSession (). Put ("msg", "Login Successful"); //get the application of Map modeActioncontext.getcontext (). Getapplication (). Put ("msg", "Login succeeded"); return"Success"; }                return"Error"; }

Method Two: Through Servletactioncontext access to SERLVETAPI, this way is not intrusive, it is recommended to use this way the action class part of the code
ImportOrg.apache.struts2.ServletActionContext;  PublicString Execute ()throwsException {if("admin". Equals (User.getusername ()) && "Admin". Equals (User.getpassword ())) {servletactioncontext.getrequest (). SetAttribute ("MSG", "Login Successful");            Servletactioncontext.getresponse (); Servletactioncontext.getpagecontext (). SetAttribute ("MSG", "Login Successful"); Servletactioncontext.getrequest (). GetSession (). SetAttribute ("MSG", "Login Successful"); return"Success"; }                return"Error"; }

Method Three: Implement the Assembly Interface, implementation method (Servletrequestaware,servletresponseaware) Action class
 Packagecom.djoker.struts2;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.apache.struts2.interceptor.ServletRequestAware;ImportOrg.apache.struts2.interceptor.ServletResponseAware; Public classLoginactionImplementsservletrequestaware,servletresponseaware{Privateuser User;  PublicUser GetUser () {returnuser; }     Public voidsetUser (user user) { This. user =user; }    PrivateHttpServletRequest request; Privatehttpservletresponse response;  PublicString Execute ()throwsException {if("admin". Equals (User.getusername ()) && "Admin". Equals (User.getpassword ())) {             This. Request.setattribute ("msg", "Login Successful"); return"Success"; }                return"Error"; } @Override Public voidSetservletresponse (httpservletresponse response) { This. Response =response; } @Override Public voidsetservletrequest (HttpServletRequest request) { This. Request =request; }    }

STRUTS2 Study Note VI: STRUTS2 's action to access Servletapi several ways

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.