Four ways to get web element request, session, application objects in STRUTS2

Source: Internet
Author: User

When we are learning web programming, we usually do a series of related operations through Requet, session, Application (ServletContext), request, session, and application They are the most commonly used and most useful objects in web development, and they can greatly facilitate development and operation of developers. But in the struts2, basically are action, these methods are not Requet, session, application, so how to get these common objects, but also become a concern for everyone, below I will demonstrate, How to get these three objects in struts2.

Way one: Get through the Actioncontext object

 Packagecom.action.day01;ImportJava.util.Map;ImportCom.opensymphony.xwork2.ActionContext;ImportCom.opensymphony.xwork2.ActionSupport;/*** Request, Session, ServletContext * with the map type is obtained by Actioncontext object *@authorAdministrator **/ Public classAccesswebelementactionextendsActionsupport {PrivateMap<string, object>request; PrivateMap<string, object>session; PrivateMap<string, object>Application;  Publicaccesswebelementaction () {Request= (map<string, object>) Actioncontext.getcontext (). Get ("Request"); Session=Actioncontext.getcontext (). GetSession (); Application=Actioncontext.getcontext (). Getapplication (); }     PublicString getwebelement () {//to save the data in RequetRequest.put ("R1", "R1"); //Save the data to the sessionSession.put ("S1", "S1"); //to save the data in applicationApplication.put ("A1", "A1"); returnSUCCESS; }}

Mode two: By implementing Requestaware, Sessionaware, applicationaware these three interface ways to get (most commonly used)

 Packagecom.action.day01;ImportJava.util.Map;ImportOrg.apache.struts2.interceptor.ApplicationAware;ImportOrg.apache.struts2.interceptor.RequestAware;ImportOrg.apache.struts2.interceptor.SessionAware;ImportCom.opensymphony.xwork2.ActionContext;ImportCom.opensymphony.xwork2.ActionSupport;/*** Take the map type of request, session, ServletContext through is to implement Requestaware, Sessionaware, * applicationaware and then pass our defined variables in, to initialize , the container automatically helps us to create, we just pass in a parameter variable into the receiving object can be * *@authorAdministrator **/ Public classAccessWebElementAction2extendsActionsupportImplementsRequestaware, Sessionaware, Applicationaware {PrivateMap<string, object>request; PrivateMap<string, object>session; PrivateMap<string, object>Application;  PublicAccessWebElementAction2 () {Request= (map<string, object>) Actioncontext.getcontext (). Get ("Request"); Session=Actioncontext.getcontext (). GetSession (); Application=Actioncontext.getcontext (). Getapplication (); }     PublicString getwebelement () {//to save the data in RequetRequest.put ("R1", "R1"); //Save the data to the sessionSession.put ("S1", "S1"); //to save the data in applicationApplication.put ("A1", "A1"); returnSUCCESS; } @Override Public voidSetapplication (map<string, object>application) {         This. Application =Application; } @Override Public voidSetsession (map<string, object>session) {         This. session =session; } @Override Public voidSetrequest (map<string, object>request) {         This. Request =request; }}

Method Three: Get HTTP type request, session, application by Servletactioncontext object

 Packagecom.action.day01;ImportJavax.servlet.ServletContext;Importjavax.servlet.http.HttpServletRequest;Importjavax.servlet.http.HttpSession;ImportOrg.apache.struts2.ServletActionContext;ImportCom.opensymphony.xwork2.ActionSupport;/*** Take HTTP type request, session, ServletContext through Servletactioncontext object to get * *@authorAdministrator **/ Public classAccessWebElementAction3extendsActionsupport {PrivateHttpServletRequest request; PrivateHttpSession session; PrivateServletContext Application;  PublicAccessWebElementAction3 () {Request=servletactioncontext.getrequest (); Session=servletactioncontext.getrequest (). GetSession (); Application=Servletactioncontext.getservletcontext (); }     PublicString getwebelement () {//to save the data in RequetRequest.setattribute ("R1", "R1"); //Save the data to the sessionSession.setattribute ("S1", "S1"); //to save the data in applicationApplication.setattribute ("A1", "A1"); returnSUCCESS; }} 

Mode four: Obtain the HttpServletRequest object by implementing the Servletrequestaware interface, and then get the session and application object through the Request object

 Packagecom.action.day01;ImportJavax.servlet.ServletContext;Importjavax.servlet.http.HttpServletRequest;Importjavax.servlet.http.HttpSession;ImportOrg.apache.struts2.ServletActionContext;ImportOrg.apache.struts2.interceptor.ServletRequestAware;ImportCom.opensymphony.xwork2.ActionSupport;/*** Take HTTP type request, session, ServletContext by implementing Servletrequestaware interface to get * *@authorAdministrator **/ Public classAccessWebElementAction4extendsActionsupportImplementsServletrequestaware {PrivateHttpServletRequest request; PrivateHttpSession session; PrivateServletContext Application;  PublicAccessWebElementAction4 () {Session=request.getsession (); Application=Request.getservletcontext (); }     PublicString getwebelement () {//to save the data in RequetRequest.setattribute ("R1", "R1"); //Save the data to the sessionSession.setattribute ("S1", "S1"); //to save the data in applicationApplication.setattribute ("A1", "A1"); returnSUCCESS; } @Override Public voidsetservletrequest (HttpServletRequest request) {//use our defined properties to get the HttpServletRequest object that the container automatically creates         This. Request =request; }}

At this point, in the Struts2 way, get request, Sessioin, Application object Three methods have been explained, there are insufficient places, I hope you have a lot of advice!

Four ways to get web element request, session, application objects in STRUTS2

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.