struts2获取request、session、application的四种方式

struts2获取request、session、application的四种方式

//获取map类型的requestsessionapplication

public class LoginAction1 extendsActionSupport {

private Map request;

private Map session;

private Map application;

publicLoginAction1() {

request= (Map)ActionContext.getContext().get("request");

session= ActionContext.getContext().getSession();

application= ActionContext.getContext().getApplication();

}

publicString execute() {

request.put("r1","r1");

session.put("s1","s1");

application.put("a1","a1");

returnSUCCESS;

}

}

//采用接口方式获得map类型的requestsessionapplication

public class LoginAction2 extendsActionSupport implements RequestAware,SessionAware, ApplicationAware {

private Map<String, Object> request;

private Map<String, Object> session;

private Map<String, Object> application;

//DIdependency injection

//IoCinverse of control

publicString execute() {

request.put("r1","r1");

session.put("s1","s1");

application.put("a1","a1");

returnSUCCESS;

}

@Override

publicvoid setRequest(Map<String, Object> request) {

this.request= request;

}

@Override

publicvoid setSession(Map<String, Object> session) {

this.session= session;

}

@Override

publicvoid setApplication(Map<String, Object> application) {

this.application= application;

}

}

//获取Http类型的requestsessionapplication

public class LoginAction3 extendsActionSupport {

private HttpServletRequest request;

private HttpSession session;

private ServletContext application;

publicLoginAction3() {

request= ServletActionContext.getRequest();

session= request.getSession();

application= session.getServletContext();

}

publicString execute() {

request.setAttribute("r1","r1");

session.setAttribute("s1","s1");

application.setAttribute("a1","a1");

returnSUCCESS;

}

}

//采用接口方式获取Http类型的requestsessionapplication

public class LoginAction4 extendsActionSupport implements ServletRequestAware {

private HttpServletRequest request;

private HttpSession session;

private ServletContext application;

publicString execute() {

request.setAttribute("r1","r1");

session.setAttribute("s1","s1");

application.setAttribute("a1","a1");

returnSUCCESS;

}

@Override

publicvoid setServletRequest(HttpServletRequest request) {

this.request= request;

this.session= request.getSession();

this.application= session.getServletContext();

}

}

转载于:https://my.oschina.net/renault/blog/487752

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值