Several ways to get httpservletrequest,httpsession in Struts2

Source: Internet
Author: User

Transferred from: http://www.kaifajie.cn/struts/8944.html

Package Com.log;import Java.io.ioexception;import Java.util.map;import javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Org.apache.struts2.servletactioncontext;import Org.apache.struts2.strutsstatics;import Org.apache.struts2.interceptor.requestaware;import Org.apache.struts2.interceptor.servletresponseaware;import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.actionsupport;import com.opensymphony.xwork2.ognl.ognlvaluestack;/** * 1, using the STRUTS2 aware interceptor * requires the action class to implement the appropriate interceptor interface. If we want to get HttpServletResponse object, we need to implement ORG.APACHE.STRUTS2. * Interceptor. The Servletresponseaware interface Myaction implements a Servletresponseaware interface, * and implements the Setservletresponse method. If an action class implements the Servletresponseaware interface, * Struts2 calls the Setservletresponse method before invoking the Execute method, and passes the response parameter to the method. * If you want to get HttpServletRequest, httpsession and cookies, the action class can implement interfaces such as Servletrequestaware, * sessionaware, and Cookiesaware, respectively. These interfaces are in the Org.apache.struts2.interceptor package. * If you want to get the request parameters, the actionClass can implement Org.apache.struts2.interceptor. * Parameteraware interface, but if you want to determine whether a parameter exists, you can also implement Com.opensymphony.xwork2.interceptor . * Parameternameaware interface. This interface has a Acceptableparametername method, which is called once when STRUTS2 obtains a request parameter. The reader can record all request parameters in this method for later use. This method is defined as follows: Boolean * Acceptableparametername (String parametername) * * @author Wei * @time October 5, 2016 afternoon 5:20:15 */class M Yaction extends Actionsupport implements Servletresponseaware {/** * */private static final long Serialversionuid = 1L;PR Ivate javax.servlet.http.HttpServletResponse response;//get HttpServletResponse object public void Setservletresponse ( HttpServletResponse response) {this.response = response;} Public String Execute () throws Exception {Response.getwriter (). Write ("Implement Servletresponseaware Interface"); return null;}} /* * Using Requestaware interceptors * is similar to the 1th method. The action class needs to implement a Org.apache.struts2.interceptor.RequestAware interface. * The difference is that Requestaware will get a Com.opensymphony.xwork2.util.OgnlValueStack object, * This object can get response, request and some other information */class Firstaction extends Actionsupport implementsRequestaware {/** * */private static final long serialversionuid = 1l;private Map request;private httpservletresponse Res ponse; @Overridepublic void Setrequest (Map request) {this.request = Request;} Public String Execute () throws Exception {java.util.set<string> keys = Request.keyset ();//enumerates all key values. There is actually only one key:struts.valueStackfor (String key:keys) System.out.println (key);//Get Ognlvaluestack object Ognlvaluestack stack = (ognlvaluestack) request.get ("Struts.valuestack");//Get HttpServletResponse Object response = (HttpServletResponse) Stack.getcontext (). Get (Strutsstatics.http_response), Response.getwriter (). Write ("Implement Requestaware Interface"); return null;}} Four ways to get httpservletrequest,httpservletresponse,httpsession in/** * struts2 * * @author Wei * @time October 5, 2016 afternoon 5:23:02 */pu Blic class LoginAction333 {public static void main (string[] args) throws IOException {}public void method3 () {/* * 3, using act Ioncontext class, this is very convenient * we can get the corresponding object by the Get method of the Org.apache.struts2.ActionContext class. The code is as follows: */httpservletresponse responsE = (httpservletresponse) actioncontext.getcontext (). get (Org.apache.struts2.StrutsStatics.HTTP_RESPONSE); HttpServletRequest request = (HttpServletRequest) actioncontext.getcontext (). Get ( Org.apache.struts2.StrutsStatics.HTTP_REQUEST);} public void Method4 () throws IOException {/* * 4, the simplest one, using the Servletactioncontext class * STRUTS2 provides us with one of the simplest ways to get httpservletresponse and other objects. This is org.apache.struts2 *. Servletactioncontext class. We can use the getrequest, * GetResponse method of the Servletactioncontext class to get HttpServletRequest, HttpServletResponse objects directly. */httpservletresponse response = Servletactioncontext.getresponse (); HttpServletRequest request = Servletactioncontext.getrequest (); HttpSession session = Request.getsession (); Response.getwriter (). Write ("Hello World");}}

  

Several ways to get httpservletrequest,httpsession 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.