session怎么得到request

发布网友 发布时间:2022-04-22 20:30

我来回答

1个回答

热心网友 时间:2023-10-04 10:02

1、通过ActionContext来获得举例代码如下:

情况1:

ActionContext context = ActionContext.getContext();
Map request = (Map) context.get("request");
Map session = context.getSession();
Map application = context.getApplication();

情况2:

ActionContext actionContext = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) actionContext
.get(ServletActionContext.HTTP_REQUEST);
HttpSession session = request.getSession();
ServletContext context = (ServletContext) actionContext
.get(ServletActionContext.SERVLET_CONTEXT);

2、通过实现 RequestAware, SessionAware,ApplicationAware等接口来获得,举例代码如下:

public class LoginAction2 implements Action, RequestAware, SessionAware,
ApplicationAware {

private Map request;
private Map session;
private Map application;

public void setRequest(Map request) {
this.request = request;
}

public void setSession(Map session) {
this.session = session;
}

public void setApplication(Map application) {
this.application = application;
}



3、通过ServletActionContext来获得,举例代码如下:

// 利用ServletActionContext获得request
HttpServletRequest request = ServletActionContext.getRequest();
// 利用request获得session
HttpSession session = request.getSession();
// 利用ServletActionContext获得servletContext
ServletContext context = ServletActionContext.getServletContext();

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com