Struts2+DAO層實現實例03——添加監聽器跟蹤用戶行為
阿新 • • 發佈:2017-06-23
map equal chan lec ack end 同時 ces per
實例說明
根據上兩次的成品進行二次加工。
加入Listener,監聽用戶的登陸註銷情況。
所用知識說明
采用SessionBindingListener對Session進行監聽。
同時,Action中獲取Application,Session,request的方法(在此只列出一種)更多方法
public class LoginAction { private Map request; private Map session; private Map application;public String execute() { request = (Map)ActionContext.getContext().get("request"); session = ActionContext.getContext().getSession(); application = ActionContext.getContext().getApplication(); request.put("username1", "jingjing1"); session.put("username2", "jingjing2"); application.put("username3", "jingjing3"); return "success"; } }
代碼實例
登陸控制UserManagment
package UserBlock; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import javafx.application.Application;import org.apache.struts2.views.util.ContextUtil; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; import java.util.ArrayList; import java.util.Map; /** * Servlet監聽器,控制記錄用戶的登陸註銷信息 * Created by Richard on 2017/6/16. */ public class UserManagment { class Userlistener implements HttpSessionBindingListener{ private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) { ActionContext context = ActionContext.getContext(); Map application = context.getApplication(); ArrayList online= (ArrayList) application.get("online"); if(online==null){ online=new ArrayList(); } online.add(username); application.put("online",online); } @Override public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) { ActionContext context = ActionContext.getContext(); Map application = context.getApplication(); ArrayList online= (ArrayList) application.get("online"); online.remove(username); } } /* 登陸 首先判別是否登陸 已經登陸---->return 沒有登陸---->獲取對應的Session,存入對應用戶名的listener */ public void login(String username){ if(islogin(username)){ return; }else{ Userlistener newUser=new Userlistener(); newUser.setUsername(username); ActionContext actionContext=ActionContext.getContext(); Map session=actionContext.getSession(); session.put("username",newUser); } } /* 判斷是否登陸: 判別條件Session中是否有對應的該用戶名的Listener 有--->已經登陸,未註銷 無--->沒有登陸 */ public boolean islogin(String username){ ActionContext actionContext=ActionContext.getContext(); Map session=actionContext.getSession(); Userlistener judge= (Userlistener) session.get("username"); if(judge!=null){ return true; }else { return false; } } /* 註銷 首先判斷是否登陸 已經登陸--->移除Listener--->true 沒有登陸--->false */ public boolean logoff(String username){ if(islogin(username)){ ActionContext actionContext=ActionContext.getContext(); Map session=actionContext.getSession(); session.remove(username); return true; }else { return false; } } /* 人數統計 返回Session中List的Size。 */ public int returnNum(){ ActionContext actionContext=ActionContext.getContext(); Map session=actionContext.getSession(); ArrayList online= (ArrayList) session.get("online"); if(online==null){ online=new ArrayList(); } return online.size(); } /* list返回 */ public ArrayList returnlist(){ ActionContext actionContext=ActionContext.getContext(); Map session=actionContext.getSession(); ArrayList online= (ArrayList) session.get("online"); if(online==null){ online=new ArrayList(); } return online; } }
主頁in.jsp
<%-- Created by IntelliJ IDEA. User: Richard Date: 2017/6/16 Time: 21:38 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Welcome</title> </head> <body> <h1>WelCome to My Struts Page</h1> <hr> 當前登錄的用戶:${param.username}<br> <hr> 當前所有登陸的用戶: <table border=1 width=200> <s:iterator value="#application.online" var="user"> <tr <s:if test="#user.odd"> style="background-color: dimgray" </s:if> > <td><s:property value="#user.count"></s:property> </td> <td><s:property value="#user"></s:property></td> </tr> </s:iterator> </table> <a href="login.jsp">註銷</a> </body> </html>
Action:
package UserBlock; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; import java.util.Map; import java.util.logging.LogManager; /** * Created by Richard on 2017/6/16. * 繼承ActionSupport實現一個Action * 登陸界面通過loginuser.action調用login()方法來處理數據 * login()方法中通過ActionContext調用Session對象,存入輸入錯誤的數據 * 通過addActionMessage()方法向頁面輸出相關信息 * 註冊頁面通過reguser.action調用reg()方法來處理數據 */ public class UserAction extends ActionSupport { private String INDEX="index"; private String LOGIN="login"; private String REG="register"; private String username; private String password; private String compassword; private UserDao user; private UserManagment managment; public String getCompassword() { return compassword; } public void setCompassword(String compassword) { this.compassword = compassword; } 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; } public String login(){ try{ managment=new UserManagment(); user=new UserDao(); ArrayList result=user.up_select(username); if(result.size()>0){ User aim= (User) result.get(0); if(aim.getPassword().equals(password)){ /*登陸成功*/ managment.login(username); return INDEX; }else{ ActionContext applicton=ActionContext.getContext(); Map session=applicton.getSession(); int count; if(session.get("count")==null){ count=0; }else{ count= (int) session.get("count"); } if(count>=3){ addActionMessage("錯誤次數過多"); count=0; session.put("count",count); return LOGIN; }else{ count++; addActionMessage("您輸入的用戶名或密碼錯誤"+count); session.put("count",count); return LOGIN; } } }else{ addActionMessage("該用戶不存在,已經跳轉到註冊頁面"); return REG; } }catch (Exception e){ addActionError(e.getMessage()); System.out.println(e.getMessage()); e.printStackTrace(); return LOGIN; } } public String reg(){ try{ managment=new UserManagment(); user=new UserDao(); ArrayList result=user.up_select(username); if(result.size()>0) { addActionMessage("該用戶已經存在"); return REG; } else{ if(user.insert(username,password)){ managment.login(username); return INDEX; }else{ addActionMessage("發生未知錯誤,請重試!"); return REG; } } }catch (Exception e){ addActionError(e.getMessage()); return REG; } } }
Struts2+DAO層實現實例03——添加監聽器跟蹤用戶行為