How to get request,session,application in Struts2

Source: Internet
Author: User

The first type:
Get the map type Request,session,application
Write in the Java file

Package com.xjtu.st; Import Java.util.Map; Import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.ActionSupport; public class Loginaction extends actionsupport{    private MAP request;    Private Map session;    Private MAP Application;    Public loginaction () {        request = (MAP) actioncontext.getcontext (). Get ("request");        Session = Actioncontext.getcontext (). getsession ();        application = Actioncontext.getcontext (). Getapplication ();    }         Public String Execute () {        request.put ("rs", "RS");        Session.put ("ss", "SS");        Application.put ("as", "as");                 return SUCCESS;}    }

Actioncontext.getcontext () is the context in which the action is obtained, and then the request for the map type can be obtained.
Parameters can be obtained on the output page

<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%><%@ taglib uri= "/struts-tags" prefix= "s"% ><%string path = Request.getcontextpath ();                    String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <title>my JSP ' myjsp.<a href= "http://www.2cto.com/kf/web/jsp/" target= "_blank" class= "Keylink" >jsp< /a> ' starting page</title> <meta http-equiv= "pragma" content= "No-cache" > <meta http-equiv= "ca  Che-control "content=" No-cache "> <meta http-equiv=" Expires "content=" 0 "> <meta http-equiv=" keywords "    Content= "Keyword1,keyword2,keyword3" > <meta http-equiv= "description" content= "This is my page" > <!-- <link rel= "stylesheet" type= "Text/css" href= "Styles.css" >-Hello <br> <s:property va Lue= "#request. Rs" > <br><!--//value by tag-<s:property value= "#session. SS" > <br> <s:property value= "#application. as" > <br> <%=request.getattribute ("rs")%><br><!-- Use the original method to value--<%=session.getattribute ("ss")%><br> <%=application.getattribute ("as")%><br > <!--html></s:property></s:property></s:property>

  Second:
implements the Requestaware,sessionaware,applicationaware interface, which becomes Di (Dependency injection) or IOC (inversion of control). The code is as follows

Package com.xjtu.st; Import Java.util.Map; Import Org.apache.struts2.interceptor.applicationaware;import Org.apache.struts2.interceptor.requestaware;import Org.apache.struts2.interceptor.SessionAware; Import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.ActionSupport; public class Loginaction extends Actionsupport implements requestaware,sessionaware,applicationaware{private Map Reque    St    Private MAP session;              Private MAP Application;        Public String Execute () {request.put ("rs", "RRs");        Session.put ("ss", "SSS");                 Application.put ("as", "AAs");    return SUCCESS; } public void Setrequest (map<string, object= "" > Request) {//TODO auto-generated method stubs this             . request=request; } public void Setsession (map<string, object= "" > Session) {//TODO auto-generated method stubs this    . session= session; } public void Setapplication (map<string, object= "" "&Gt    Application) {//TODO auto-generated method Stub this.application = Application; }}</string,></string,></string,>

The first 2 types are obtained by the map type, the latter two are the methods to obtain the Htteservletrequest,httpsession,servletcontext type of the Third kind:

The code is as follows

Package com.xjtu.st; Import Java.util.Map; Import Javax.servlet.servletcontext;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.HttpSession; Import Org.apache.struts2.servletactioncontext;import Org.apache.struts2.interceptor.applicationaware;import Org.apache.struts2.interceptor.requestaware;import Org.apache.struts2.interceptor.SessionAware; Import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.ActionSupport;    public class Loginaction extends Actionsupport {private httpservletrequest request;    Private HttpSession session;         Private ServletContext Application;        Public loginaction () {request= servletactioncontext.getrequest ();        Session = Request.getsession ();    application = Session.getservletcontext ();        } public String Execute () {Request.setattribute ("rs", "Rrrs");        Session.setattribute ("ss", "SSSs");                 Application.setattribute ("as", "AAAS");    return SUCCESS; }        }

This method is not commonly used in the fourth type:
is also the way of the IOC
The code is as follows
Package com.xjtu.st; Import Java.util.Map; Import Javax.servlet.servletcontext;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.HttpSession; Import Org.apache.struts2.servletactioncontext;import Org.apache.struts2.interceptor.applicationaware;import Org.apache.struts2.interceptor.requestaware;import Org.apache.struts2.interceptor.servletrequestaware;import Org.apache.struts2.interceptor.SessionAware; Import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.ActionSupport;    public class Loginaction extends Actionsupport implements servletrequestaware{private HttpServletRequest request;    Private HttpSession session;          Private ServletContext Application;        Public String Execute () {Request.setattribute ("rs", "Rrrrs");        Session.setattribute ("ss", "Sssss");                 Application.setattribute ("as", "Aaaas");    return SUCCESS; } public void Setservletrequest (HttpServletRequest request) {//TODO Auto-geneRated method stub this.request = Request;        Session = Request.getsession ();             application = Session.getservletcontext (); }      }

How to get request,session,application 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.