在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ Servlet HttpSessionEvent統(tǒng)計在線用戶數(shù)實(shí)例
Servlet web.xml welcome-file-list
Servlet從數(shù)據(jù)庫讀取記錄性能優(yōu)化
Servlet URL重寫帶參數(shù)
War文件
Web技術(shù)基礎(chǔ)
Servlet GenericServlet類
Servlet API
Servlet ServletInputStream類
使用MyEclipse創(chuàng)建Servlet
Servlet增刪改查
Servlet ServletConfig配置信息
Servlet Cookies
Servlet重定向
Servlet生命周期
Servlet HttpSession會話
Servlet HttpServlet類
Servlet注冊表單示例
Servlet表單隱藏字段
Servlet下載文件
Servlet教程
Servlet身份驗證過濾器
Servlet ServletOutputStream類
Servlet HttpSession登錄注銷實(shí)例
Servlet啟動時加載
Servlet事件和監(jiān)聽器
使用Eclipse創(chuàng)建Servlet
Servlet ServletContextEvent事件
Servlet HttpSessionEvent統(tǒng)計在線用戶數(shù)實(shí)例
Servlet RequestDispatcher請求轉(zhuǎn)發(fā)
Servlet使用注釋
Servlet過濾器示例
Servlet過慮器
Servlet ServletContext配置信息
Servlet登錄注銷Cookies實(shí)例
Servlet工作流程
Servlet會話跟蹤
Servlet登錄實(shí)例
Servlet ServletRequest接口
Servlet ServletRequestEvent類和接口
Servlet入門程序
Servlet查詢搜索數(shù)據(jù)示例
Servlet FilterConfig應(yīng)用示例
Servlet顯示所有頭信息
Servlet屬性設(shè)置
使用NetBeans創(chuàng)建Servlet
Servlet接口實(shí)現(xiàn)
Servlet上傳文件

Servlet HttpSessionEvent統(tǒng)計在線用戶數(shù)實(shí)例

會話對象更改時通知HttpSessionEvent,此事件的相應(yīng)偵聽器接口是HttpSessionListener。

我們可以在這個事件上執(zhí)行一些操作,例如:計數(shù)統(tǒng)計當(dāng)前登錄的用戶,記錄登錄時間,注銷時間等用戶詳細(xì)信息。

HttpSessionListener接口的方法

HttpSessionListener接口中聲明了兩種方法,這些方法必須由servlet程序員來實(shí)現(xiàn),以執(zhí)行某些操作。

  • public void sessionCreated(HttpSessionEvent e):在創(chuàng)建會話對象時被調(diào)用。
  • public void sessionDestroyed(ServletContextEvent e):當(dāng)會話無效時被調(diào)用。

HttpSessionEvent和HttpSessionListener統(tǒng)計當(dāng)前登錄用戶數(shù)的示例

在這個例子中,實(shí)現(xiàn)對當(dāng)前登錄的用戶進(jìn)行統(tǒng)計。主要創(chuàng)建了以下幾個代碼文件:

  • index.html:從用戶處獲取輸入。
  • MyListener.java:一個偵聽器類,用于計算當(dāng)前登錄的用戶數(shù)量,并將此信息作為屬性存儲在ServletContext對象中。
  • First.java:一個創(chuàng)建會話并打印登錄用戶數(shù)量的Servlet類。
  • Logout.java:一個使會話無效的Servlet類。

打開Eclipse,創(chuàng)建一個動態(tài)Web項目:HttpSessionEvent,其完整的目錄結(jié)構(gòu)如下所示 -

以下是這個項目中的幾個主要的代碼文件。

文件:index.html -

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>統(tǒng)計在線用戶數(shù)量</title>
</head>
<body>
    <div style="font-align: center;">
        <form action="first" method="post">
            用戶名:<input type="text" name="username" value="admin">密碼:<input
                type="password" name="password"><input
                type="submit" value="登錄" />
        </form>
    </div>
</body>
</html>

監(jiān)聽器文件:CountUserListener.java -

package com.yiibai;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class CountUserListener implements HttpSessionListener {
    ServletContext ctx = null;
    static int total = 0, current = 0;

    public void sessionCreated(HttpSessionEvent e) {
        total++;
        current++;

        ctx = e.getSession().getServletContext();
        ctx.setAttribute("totalusers", total);
        ctx.setAttribute("currentusers", current);

    }

    public void sessionDestroyed(HttpSessionEvent e) {
        current--;
        ctx.setAttribute("currentusers", current);
    }

}

文件:First.java -

package com.yiibai;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FirstServlet extends HttpServlet {
    // 顯示用戶數(shù)
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        // retrieving data from ServletContext object
        ServletContext ctx = getServletContext();
        int t = (Integer) ctx.getAttribute("totalusers");
        int c = (Integer) ctx.getAttribute("currentusers");
        out.print("<br>用戶總數(shù): " + t);
        out.print("<br>當(dāng)前用戶數(shù): " + c);
        out.print("<br><a href='logout'>注銷</a>");
        out.close();
    }

    // 執(zhí)行用戶登錄
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        String n = request.getParameter("username");
        String password = request.getParameter("password");
        // 假設(shè)輸入密碼為:123456時,此用戶登錄成功
        if (password == null) {
            password = "";
        }
        if (password.equals("123456")) {
            out.print("您好, " + n);
            HttpSession session = request.getSession();
            session.setAttribute("uname", n);
        } else {
            out.print("登陸失敗 ~");
        }
        // retrieving data from ServletContext object
        ServletContext ctx = getServletContext();
        int t = (Integer) ctx.getAttribute("totalusers");
        int c = (Integer) ctx.getAttribute("currentusers");
        out.print("<br>用戶總數(shù): " + t);
        out.print("<br>當(dāng)前用戶數(shù): " + c);
        out.print("<br><a href='logout'>注銷</a>");
        out.close();
    }

}

文件:LogoutServlet.java -

package com.yiibai;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LogoutServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(false);
        session.invalidate();// invalidating session

        out.print("You are successfully logged out");

        out.close();
    }

}

文件:web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>HttpSessionEvent</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
        <listener>
        <listener-class>com.yiibai.CountUserListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>com.yiibai.FirstServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/first</url-pattern>
    </servlet-mapping>    
    <servlet>
        <servlet-name>LogoutServlet</servlet-name>
        <servlet-class>com.yiibai.LogoutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LogoutServlet</servlet-name>
        <url-pattern>/logout</url-pattern>
    </servlet-mapping>
</web-app>

在編寫上面代碼后,部署此Web應(yīng)用程序(在項目名稱上點(diǎn)擊右鍵->”Run On Server…”),打開瀏覽器訪問URL: http://localhost:8080/HttpSessionEvent/ ,如果沒有錯誤,應(yīng)該會看到以下結(jié)果 -

打開另一個瀏覽器訪問URL: http://localhost:8080/HttpSessionEvent/ ,登錄應(yīng)用,然后注銷,再次訪問: http://localhost:8080/HttpSessionEvent/first ,應(yīng)該看到以下結(jié)果 -