package action; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {
private static final long serialVersionUID = 1L; private String username; private String password; private HttpServletRequest httpServletRequest; private HttpServletResponse httpServletResponse; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @SuppressWarnings("unchecked") public String execute() throws Exception { if(!"admin".equals(username) || !"admin".equals(password)) { addFieldError("username", "用户名或密码错误!"); return "failure"; } else { Map map = ActionContext.getContext().getSession(); ActionContext.getContext().put("ActionContext", "ActionContext方式耦合"); map.put("user", "invalid"); httpServletRequest.setAttribute("Aware", "Aware方式耦合"); Cookie cookie = new Cookie("username", this.getUsername()); //设置cookie最大存活时间 //如果设置为正数,就是相应的存活时间;如果设置为负数,当浏览器被关闭的是,cookie就会被删除 cookie.setMaxAge(1000); //第一次执行,cookie不会显示 httpServletResponse.addCookie(cookie); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("ServletActionContext", "ServletActionContext方式耦合"); return "success"; } } public void validate() { if(null == username || "".equals(username)) { addFieldError("username", "请填写用户名!"); } if(null == password || "".equals(password)) { addFieldError("password", "请填写密码!"); } } @SuppressWarnings("unchecked") public String login() throws Exception { if(!"admin".equals(username) || !"admin".equals(password)) { addFieldError("username", "用户名或密码错误!"); return "failure"; } else { Map map = ActionContext.getContext().getSession(); ActionContext.getContext().put("ActionContext", "ActionContext方式耦合"); map.put("user", "invalid"); httpServletRequest.setAttribute("Aware", "Aware方式耦合"); Cookie cookie = new Cookie("username", this.getUsername()); //设置cookie最大存活时间 //如果设置为正数,就是相应的存活时间;如果设置为负数,当浏览器被关闭的是,cookie就会被删除 cookie.setMaxAge(1000); //第一次执行,cookie不会显示 httpServletResponse.addCookie(cookie); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("ServletActionContext", "ServletActionContext方式耦合"); return "success"; } } //这个方法被自动执行,是依赖注入 //这是使用servlet的方法 @Override public void setServletRequest(HttpServletRequest httpServletRequest) { // TODO Auto-generated method stub this.httpServletRequest = httpServletRequest; } @Override public void setServletResponse(HttpServletResponse httpServletResponse) { // TODO Auto-generated method stub this.httpServletResponse = httpServletResponse; } } |