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();