action获取session的方式
1.解耦合的方式
public String execute() {
if("jbit".equals(username) && "bdqn".equals(password)) {
Map<String,Object> session = null;
session = ActionContext.getContext().getSession();
if(session.containsKey(CURRENT_USER)) {
session.remove(CURRENT_USER);
}
session.put(CURRENT_USER, username);
return "success";
} else {
return "fail";
}
}
2.耦合的方式获取
public String execute() {
if("jbit".equals(username) && "bdqn".equals(password)) {
HttpSession session = null;
session = ServletActionContext.getRequest().getSession();if(session.getAttribute(CURRENT_USER) != null) {
session.removeAttribute(CURRENT_USER);
}
session.setAttribute(CURRENT_USER, username);
return "success";
} else {
return "fail";
}
}