日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

java监听数据库操作_第十六篇——JDBC操作数据库之监听器

發(fā)布時間:2024/8/23 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java监听数据库操作_第十六篇——JDBC操作数据库之监听器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。