session怎么得到request
1个回答
展开全部
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();
情况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();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询