判断用户是否在线
判斷用戶是否在線是通過對HTTP會話的監聽來實現的
通過繼承HttpSessionBindingListener接口實現對HtTTP會話的監聽。繼承該接口的類必須實現繼承的抽象方法valueBound(HttpSessionBindingEvent)向session中存入實現該接口的對象時觸發和valueUnbound(HttpSessionBindingEvent)從session移除該對象的觸發
創建監聽HTTP會話的OnLine類文件
創建UserLogOn類文件,其中的checkuser()方法用來驗證用戶信息并返回一個boolean類型值
package com.zj.Online.test;import javax.servlet.http.HttpSession; public class UserLogOn {private String username;private String backstr="";private OnLine on=new OnLine();public UserLogOn(){}public void setUsername(String username){this.username=username;}public String getUsername(){return this.username;}public boolean checkuser(HttpSession session){boolean mark=true;if(this.username==null||this.username.equals("")){this.backstr="<li>請輸入<b>用戶名!</b></li><br>";mark=false;}if(mark){on.setUsername(this.username);session.setAttribute("onlineuser",on); //將Online監聽類對象存入session中mark=on.getMark();if(!mark)this.backstr="該用戶已在線!";}return mark; }public String getBackstr(){return this.backstr;} }創建首頁面index.jsp供用戶輸入登錄信息
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="dologon.jsp"><table bgcolor="lightgrey" height="30"><tr><td align="30">用戶名:<input type="text" name="username" size="30"></td></tr><tr bgcolor="lightgrey"><td><input type="submit" name="logon" value="登錄"><input type="reset" name="clear" value="重置"></td></tr></table></form> </body> </html>創建接收Form表單的頁面dologon.jsp,在該頁面中調用UserLogOn類中的checkuser()方法來驗證用=用戶身份。若輸入的用戶名不為空并且該用戶不在線,轉向myline.jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><<jsp:useBean id="mylogon" class="com.zj.Online.test.UserLogOn"></jsp:useBean> <%String username=request.getParameter("username");if(username==null)username="";username=new String(username.getBytes("ISO-8859-1"),"gbk");mylogon.setUsername(username); //設置用戶名if(mylogon.checkuser(session)){ //如果用戶名不為空并且該用戶不在線session.setAttribute("username",username);response.sendRedirect("myline.jsp");} %> <html><head><title>判斷用戶是否在線</title><link rel="stylesheet" type="text/css" href="css/style.css"></head><body><center><table style="margin-top:200" width="250" border="1" cellpadding="0" cellspacing="0" bordercolor="black" bordercolorlight="black" bordercolordark="white"><tr bgcolor="lightgrey" height="30"><td align="center">登錄狀況</td></tr><tr height="50"><td align="center"><%=mylogon.getBackstr()%></td></tr></table><a href="index.jsp">[返回]</a></center></body> </html>創建myline.jsp頁面,顯示用戶的在線情況
<%@ page contentType="text/html;charset=GBK"%> <%@ page import="java.util.*"%> <%session.setMaxInactiveInterval(10);Vector vec=(Vector)application.getAttribute("online"); %> <html><head><meta http-equiv="refresh" content="12"><title>判斷用戶是否在線</title><link rel="stylesheet" type="text/css" href="css/style.css"></head> <body><center><table style="margin-top:200" width="220" height="100" border="1" cellpadding="0" cellspacing="0" bordercolor="black" bordercolorlight="black" bordercolordark="white"><tr bgcolor="lightgrey" height="25"><td align="center" colspan="2">在線用戶</td></tr><tr><td width="60%" align="center" valign="middle">10秒內沒有發言<br>您將被視為下線</td></tr><tr><td align="center" valign="middle"><%if(vec==null||vec.size()==0)out.println("沒有用戶!");else{if(vec.contains(session.getAttribute("username"))){out.println("您已經上線!用戶名:"+session.getAttribute("username"));}elseout.println("您沒有上線!");}%></td> </tr><tr bgcolor="lightgrey"><td align="center" height="25" colspan="2"><%if(session.getAttribute("username")==null)out.println("您已經下線!");elseif(vec.contains(session.getAttribute("username")))out.println("<a href='myline.jsp'>[發言]</a>");elseout.println("請先登錄!");%></table><%if(!vec.contains(session.getAttribute("username")))out.println("<a href='index.jsp'>[登錄]</a>");%> </center></body> </html>補充:本列另外設置一個Vector對象,并將它存入Application對象中,用來存儲在線的用戶。當用戶登錄時,如果用戶名存在于Vector對象中,則提示“該用戶在線”,否則將該用戶名存入Vector對象中;當用戶下線時,從Vector對象中移除該用戶名。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: 应用session对象实现用户登录
- 下一篇: 转换输入文本中的回车和空格