国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - servlet監聽實現統計在線人數功能 附源碼下載

servlet監聽實現統計在線人數功能 附源碼下載

2020-09-11 10:34阿木俠 Java教程

這篇文章主要為大家詳細介紹了servlet監聽統計在線人數的實現方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了servlet統計在線人數的具體代碼,供大家參考,具體內容如下

ServletContext事件監聽器---->針對applicationScope
ServletContextListener(*)
對整個Web應用的裝載和卸載進行監聽。
 ServletContextAttributeListener
對ServletContext中的信息存放、刪除和替換進行監聽。
ServletContext就是Servlet上下文監聽,在web中表示的是對啟動服務和銷毀服務進行監聽,需要實現的接口:
ServletContextListener接口,實現的就是對上下午進行監聽:
void contextInitialized(ServletContextEvent sce):啟動上下文時的監聽
void contextDestroyed(ServletContextEvent sce):銷毀上下文時進行的監聽
除了對上下文的啟動和銷毀進行監聽的之外,還可以對上下文的屬性進行監聽:ServletContextAttributeListener接口。
void attributeAdded(ServletContextAttributeEvent event):設置上下文屬性監聽
void attributeRemoved(ServletContextAttributeEvent event):移除上下文屬性的監聽
void attributeReplaced(ServletContextAttributeEvent event):修改上下文屬性的監聽
ServletContextAttributeEvent:事件,可以通過事件取得屬性的內容和名稱。
·取得屬性名稱:public java.lang.String getName()
·取得屬性的值:public java.lang.Object getValue()

效果如下圖:

當登錄一個賬號時

servlet監聽實現統計在線人數功能 附源碼下載

打開另一個瀏覽器,再登錄一個賬號

servlet監聽實現統計在線人數功能 附源碼下載

如上圖,我們可以看到,程序已經完成了統計在線人數和顯示人員列表的功能,那么他的實現流程是什么呢?

我們可以通過ServletContextListener完成在線人數的統計和顯示在線人數列表,首先listener和filter一樣要在web.xml中進行描述。

代碼如下:

?
1
2
3
<listener>
 <listener-class>net.jvsun.ListenerTest</listener-class>
</listener>

為了測試這個程序,我們也必須完成用戶登錄功能。
數據庫連接幫助類:

?
1
2
3
4
5
6
7
8
9
10
11
public class JDBCHelper {
 public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
 public static final String URL = "jdbc:oracle:thin:@localhost:1521:xxx";
 public static final String DBNAME = "scott";
 public static final String PASSWORD = "xxx";
 public static Connection getConn() throws Exception{
 Class.forName(DRIVER);
 Connection conn = DriverManager.getConnection(URL, DBNAME, PASSWORD);
 return conn;
 }
}

用戶實體類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class UserPOJO implements Serializable{
 private static final long serialVersionUID = 7554548269035753256L;
 private int id;
 private String username;
 private String password;
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 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 UserPOJO(int id, String username, String password) {
 super();
 this.id = id;
 this.username = username;
 this.password = password;
 }
 public UserPOJO(String username, String password) {
 this.username = username;
 this.password = password;
 }
 public UserPOJO() {
 super();
 // TODO Auto-generated constructor stub
 }
 
}

數據庫處理類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class UserDAO {
 public UserPOJO login(String username, String password) {
 UserPOJO user=null;
 Connection conn = null;
 PreparedStatement pstate = null;
 ResultSet res = null;
 try {
 conn=JDBCHelper.getConn();
 String sql="select id,username from userinfo where username=? and password=?";
 pstate = conn.prepareStatement(sql);
 pstate.setString(1, username);
 pstate.setString(2, password);
 res = pstate.executeQuery();
 while(res.next()){
 user=new UserPOJO(res.getInt(1),username,null);
 }
 } catch (Exception e) {
 e.printStackTrace();
 }finally{
 try {
 res.close();
 pstate.close();
 conn.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 
 }
 return user;
 }
}

servlet類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class UserServlet extends HttpServlet{
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 String path = request.getContextPath();
 response.setContentType("text/html; charset=utf-8");
 request.setCharacterEncoding("utf-8");
 String username = request.getParameter("username");
 String password = request.getParameter("password");
 ServletContext application = this.getServletContext();//取得application對象
 List<String> list = (List<String>) application.getAttribute("allUser");
 if(list.indexOf(username) == -1){
 UserPOJO pojo = new UserDAO().login(username, password);
 if(null != pojo){
 HttpSession session = request.getSession(true);//取得session對象
 session.setAttribute("userName", username);
 }
 
 }
 response.sendRedirect(path+"/index.jsp");
 }
 
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
 throws ServletException, IOException {
 doGet(req, resp);
 }
 
}

監聽類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class ListenerTest implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener{
 ServletContext application = null;
 public void contextDestroyed(ServletContextEvent event) {
 System.out.println("服務器關閉");
 }
 
 public void contextInitialized(ServletContextEvent event) {
 List<String> list = new ArrayList<String>();
 //用來保存所有已登錄的用戶
 application = event.getServletContext();
 //取得application對象
 application.setAttribute("allUser", list);
 //將集合設置到application范圍屬性中去
 
 }
 
 public void attributeAdded(HttpSessionBindingEvent se) {
 List<String> list = (List<String>)application.getAttribute("allUser");
 //假設:用戶登陸成功之后,只將戶名設置到session中
 String userName = (String)se.getValue();
 //取得用戶名
 if(list.indexOf(userName) == -1){
 //表示此用戶之前沒有登陸
 list.add(userName);
 application.setAttribute("allUser", list);
 }
 }
 
 public void attributeRemoved(HttpSessionBindingEvent se) {
 List<String> list = (List<String>)application.getAttribute("allUser");
 list.remove((String)se.getValue());
 application.setAttribute("allUser", list);
 }
 
 public void attributeReplaced(HttpSessionBindingEvent se) {
 
 }
 
 public void sessionCreated(HttpSessionEvent event) {
 
 }
 
 public void sessionDestroyed(HttpSessionEvent event) {
 
 }
}

登錄頁面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@page contentType="text/html; charset=utf-8"%>
<%@page import="java.util.*" %>
<%
 String path = request.getContextPath();
 %>
 
<html>
 <body>
 
 <form action="<%=path %>/Login" method="post">
 賬號:
 <input type="text" name="username" />
 <br />
 密碼:
 <input type="password" name="password" />
 <br />
 <input type="submit" value="提交" />
 </form>
 
 </body>
 
</html>

顯示在線人數和在線人員的列表界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
 <title></title>
 </head>
 
 <body>
 <ul>
 <%
 System.out.println(application.getAttribute("allUser"));
 if(null != application.getAttribute("allUser")){
 List<String> list = (List<String>)application.getAttribute("allUser");
 %>
 <h2>在線人數:<%=list.size() %></h2>
 <%
 for(String s:list){
 %>
 <a>姓名:</a><%=s %><a>---->此時在線</a><br>
 
 <%
 }
 }
 %>
 </ul>
 <hr/>
 <a href="<%=path %>/logout.jsp" rel="external nofollow" >注銷</a>
 </body>
</html>

注銷界面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
 <title>logout</title>
 </head>
 <body>
 <%
 session.removeAttribute("username");
 session.invalidate();
 response.sendRedirect("login.jsp");
 %>
 </body>
</html>

代碼下載地址:servlet統計在線人數

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/weixin_36380516/article/details/70144559

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 免费一级毛片电影 | 天天澡天天狠天天天做 | 国产精品精品 | 成人欧美一区二区三区在线播放 | 精品视频网| av网站大全免费 | 精品蜜桃一区二区三区 | 天堂在线免费视频 | 情一色一乱一欲一区二区 | 久久色视频 | 日韩精品久久久久久 | 97伦理在线 | 色乱码一区二区三区网站 | 看a网站 | 一级片免费视频 | 日韩一区二区中文 | 国产高清在线精品一区二区三区 | 毛片国产 | 久久久免费网站 | 午夜成人在线视频 | 亚洲午夜电影 | 日韩精品一区二区三区在线观看 | 久久精品青青大伊人av | 色婷婷国产精品综合在线观看 | 久久久网 | 手机看片在线 | 欧美自拍一区 | 91久久精品国产 | 中文字幕啪啪 | 日韩电影中文字幕 | 韩国精品一区二区三区 | 亚洲精品久久久久久久久久久 | 久久综合电影 | 91视频免费网站 | 中文字幕免费 | 成人av视 | 国产精品国产三级国产aⅴ中文 | 在线观看欧美 | 91精品国产乱码久久久久久 | 狠狠躁夜夜躁人人爽天天天天97 | 国产精品福利91 |