基于Servlet用户统计功能实现.pdfVIP

  • 1
  • 0
  • 约8.86千字
  • 约 24页
  • 2025-10-22 发布于北京
  • 举报

实例——人员统计

人员列表是一个比较常见的功能,每当用户登录成功后,就会在列表中增加此用户名称,这

样就可以知道当前有哪些的用户。这个功能在Web中可以依靠实现,实现原理如下图所

示:

Listenerinstance-onlinenel

statistics

Theonlinepeoplelistisarelativelycommonfunction.Wheneverauserlogsin

successfully,theusernamewillbeaddedtothelist,sothatyoucanknowwhichusersare

currentlyonline.ThisfunctioncanbeimplementedintheWebbyrelyingonlisteners.

Theimplementationprincipleisshowninthefigurebelow:

绿色箭头表示页面间跳转。蓝色箭头是针对Servlet的操作。

当用户登录成功后,会向session中增加一个用户的信息标记,此时,将触发的,会向

Greenarrowsindicatejumpsweenpages.ThebluearrowsareoperationsfortheServlet

listener.

Whentheuserlogsinsuccessfully,ausersinformationtagwillbeaddedtothesession.Atthistime,thelisteningeventwillbe

triggeredandwillbesenttothesession.

用户列表中增加一个新的用户名(用户列表可以通过Set保存),当用户注销或者会话超时后,会自

动从列表中删除此用户。

由于所有的用户都需要此用户列表,那么此列表的内容就必须保存在application范围内。

另外,关于登录模块的验证机制前面的一些专题已经做得够出色了,这里我们图个方便,只要用户名

不为空,就算验证通过,就向列表中添加。

【提示】为什么要选择Set

之所以选Set而不选List,最主要的就是因为Set唯一的特性,而恰巧,用户名也必须是唯

一的、不能重复的,所以需求和Set的特性不谋而合。关于集合,自己复习。

要完成用户列表的,需要使用一下3个接口:

·ServletContextListener接口:在容器初始化时设置一个空的集合到application中。

Addanewusernametotheuserlist(theuserlistcanbesavedthroughSet).Whenthe

userlogsofforthesessiontimesout,theuserwillbeautomati

文档评论(0)

1亿VIP精品文档

相关文档