java监听数据库操作_第十六篇——JDBC操作数据库之监听器
JavaWeb應用中,很多的地方都和session有關。因此session相關的事件監(jiān)聽器,在日常工作中非常有用。
有時候我們需要統(tǒng)計當前在線的人數(shù)和訪問人數(shù)總數(shù),此時就可以使用監(jiān)聽器技術來很簡單的實現(xiàn)這種功能。
注意:
session并不是瀏覽器關閉時銷毀的,而是在session失效的時候銷毀下列代碼就是監(jiān)測session創(chuàng)建、銷毀。
銷毀session,可以設置過期時間:
1
步驟說明
2、在第十五篇基礎上繼續(xù)實現(xiàn)功能——第十六篇JDBC操作數(shù)據(jù)庫之監(jiān)聽器。
一.InitNumListener.java
對ServletContext對象進行監(jiān)聽的接口有ServletContextAttributeListener 和 ServletContext-Listener。package com.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.*;
/**
* Created by Ray on 2018/3/17 0017.
**/
public class InitNumListener implements ServletContextListener {
//初始化上下文
public void contextInitialized(ServletContextEvent sce) {
/* This method is called when the servlet context is
initialized(when the Web application is deployed).
You can initialize servlet context related data here.
*/
//創(chuàng)建接口
ServletContext servletContext = sce.getServletContext();
//訪問總數(shù)的文件路徑
String filename = servletContext.getRealPath("/WEB-INF/count.txt");
File file = new File(filename);
//初始化訪問總數(shù)
Integer totalcount = 0;
if(file.exists()){
try{
//讀取文件內(nèi)容
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
totalcount = Integer.valueOf(bufferedReader.readLine());
//關閉流
bufferedReader.close();
fileReader.close();
}catch (Exception e){
e.printStackTrace();
}
}
//設置屬性name的值為value
servletContext.setAttribute("totalcount",totalcount); //訪問總數(shù)
servletContext.setAttribute("accesscount",0); //在線用戶數(shù)
}
//銷毀上下文
public void contextDestroyed(ServletContextEvent sce) {
/* This method is invoked when the Servlet Context
(the Web application) is undeployed or
Application Server shuts down.
*/
//創(chuàng)建接口
ServletContext servletContext = sce.getServletContext();
//訪問總數(shù)的文件路徑
String filename = servletContext.getRealPath("/WEB-INF/count.txt");
File file = new File(filename);
try{
//將內(nèi)容寫入文件
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(servletContext.getAttribute("totalcount").toString());
//關閉流
bufferedWriter.close();
fileWriter.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
二.UserEnterListener.java
實現(xiàn)HttpSessionListener接口的監(jiān)聽器,可以監(jiān)聽 HttpSession 對象本身的創(chuàng)建和銷毀。package com.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Created by Ray on 2018/3/17 0017.
**/
public class UserEnterListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
/* Session is created. */
ServletContext servletContext = se.getSession().getServletContext();
//返回屬性name的值
Integer totalcount = (Integer) servletContext.getAttribute("totalcount");
Integer accesscount = (Integer) servletContext.getAttribute("accesscount");
//使用指定的屬性名字綁定一個對象并儲存到session中
servletContext.setAttribute("totalcount",totalcount+1);
servletContext.setAttribute("accesscount",accesscount+1);
}
public void sessionDestroyed(HttpSessionEvent se) {
/* Session is destroyed. */
ServletContext servletContext = se.getSession().getServletContext();
//返回屬性name的值
Integer accesscount = (Integer) servletContext.getAttribute("accesscount");
//使用指定的屬性名字綁定一個對象并儲存到session中
servletContext.setAttribute("accesscount",accesscount-1);
}
}
三.web.xml
1.監(jiān)聽器Listener
2.設置session過期時間
3.省略部分代碼(懶得刪)<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
com.listener.InitNumListener
com.listener.UserEnterListener
1
BookList
com.control.BookList
BookList
/BookList
BookAdd
com.control.BookAdd
BookAdd
/BookAdd
BookUpdate
com.control.BookUpdate
BookUpdate
/BookUpdate
BookDoUpdate
com.control.BookDoUpdate
BookDoUpdate
/BookDoUpdate
BookDelete
com.control.BookDelete
BookDelete
/BookDelete
BookDoDelete
com.control.BookDoDelete
BookDoDelete
/BookDoDelete
BookBatchAdd
com.control.BookBatchAdd
BookBatchAdd
/BookBatchAdd
BookBatchDelete
com.control.BookBatchDelete
BookBatchDelete
/BookBatchDelete
BookFind
com.control.BookFind
BookFind
/BookFind
Login
com.control.Login
Login
/Login
DrawImage
com.control.DrawImage
DrawImage
/DrawImage
Register
com.control.Register
Register
/Register
LoginUser
com.control.LoginUser
LoginUser
/LoginUser
ExitLogin
com.control.ExitLogin
ExitLogin
/ExitLogin
Upload
com.control.Upload
Upload
/Upload
ListFile
com.control.ListFile
ListFile
/ListFile
Download
com.control.Download
Download
/Download
CharSetFilter
com.Filter.CharSetFilter
charset
UTF-8
CharSetFilter
/*
404
/error/error404.jsp
500
/error/error500.jsp
ErrorFilter
com.Filter.ErrorFilter
ErrorFilter
/error.jsp
ERROR
四.listen.jsp
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
Listen訪問人數(shù):${totalcount} ?? 在線人數(shù):${accesscount}
五.count.txt(總訪問人數(shù)19)
六.頁面效果
另一個瀏覽器(模擬多個用戶)
關閉瀏覽器(模擬在線人數(shù)退出),等1-2分鐘刷新
ok!
總結
以上是生活随笔為你收集整理的java监听数据库操作_第十六篇——JDBC操作数据库之监听器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 睡眠音频分割及识别问题(十)--Java
- 下一篇: android web 访问数据库,We