jsp页面中实现在线人数功能 - 大锤资源网

jsp页面中实现在线人数功能

作者: 大锤     发布于「 技术分享 」 - 大锤资源网

2020-3-11 分类: 技术分享 阅读(108)评论(0)
当前位置:首页 > 技术分享 > 正文

作者: 大锤

全网最全的网络资源分享网站

手机扫码查看

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

首先在src目录下创建包,com.xxx(xxx随意)

接着创建类AttributeText代码如下

package com.cai;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * Application Lifecycle Listener implementation class AttributeText
 *
 */
@WebListener
public class AttributeText implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {

    /**
     * Default constructor. 
     */
    public AttributeText() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
     */
    public void attributeAdded(ServletContextAttributeEvent scae)  { 
         
    }

	/**
     * @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
     */
    public void attributeRemoved(ServletContextAttributeEvent scae)  { 
         
    }

	/**
     * @see ServletRequestAttributeListener#attributeRemoved(ServletRequestAttributeEvent)
     */
    public void attributeRemoved(ServletRequestAttributeEvent srae)  { 
         // TODO Auto-generated method stub
    }

	/**
     * @see ServletRequestAttributeListener#attributeAdded(ServletRequestAttributeEvent)
     */
    public void attributeAdded(ServletRequestAttributeEvent srae)  { 
         // TODO Auto-generated method stub
    }

	/**
     * @see ServletRequestAttributeListener#attributeReplaced(ServletRequestAttributeEvent)
     */
    public void attributeReplaced(ServletRequestAttributeEvent srae)  { 
         // TODO Auto-generated method stub
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent se)  { 
         
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent se)  { 
         
    }

	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent se)  { 
         
    }

	/**
     * @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
     */
    public void attributeReplaced(ServletContextAttributeEvent scae)  { 
         
    }
	
}

创建ServletContextListenerText代码如下

package com.cai;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class ServletContextListener
 *
 */
public class ServletContextListenerText implements javax.servlet.ServletContextListener, HttpSessionListener, ServletRequestListener {
	private static int count=0;
    /**
     * Default constructor. 
     */
    public ServletContextListenerText() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		
	}

	@Override
	public void requestDestroyed(ServletRequestEvent sre) {
		
	}

	@Override
	public void requestInitialized(ServletRequestEvent sre) {
		
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		count++;
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		count--;
	}

	public static int getCount() {
		return count;
	}
  
	
}

在WebContent目录下创建两个jsp文件(名字随意,代码如下)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="test.jsp" method="post">
<tr>
<td colspan="1">用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交">
<input type="reset" value="重置"></td>
</tr>


</form>
</body>
</html>

 

<%@page import="com.cai.ServletContextListenerText"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<%
request.setCharacterEncoding("utf-8");
%>
用户名:<%=request.getParameter("username")%><br>
当前人数为:<%=ServletContextListenerText.getCount() %>人。
</center>
</body>
</html>

在Web-INF目录下创建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" version="3.1">
  <listener>
    <listener-class>com.cai.ServletContextListenerText</listener-class>
  </listener>
</web-app>

配置web.xml文件

 

分享到:
未经允许不得转载:

作者: 大锤, 转载或复制请以 超链接形式 并注明出处 大锤资源网
原文地址: 《jsp页面中实现在线人数功能》 发布于2020-3-11

评论

切换注册

登录

您也可以使用第三方帐号快捷登录

切换登录

注册

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏