大家好,这是我第一次写博客 ,写的不好还请见谅,呵呵
我今天一直在弄关于统计在线人数和在线用户的问题,,在网上找了好多,但是没有太好的,,多优点差强人意,所以我就自己弄了弄 ,现在我把代码帖出来,让大家看看,,。。。。
首先:jsp ----------------------------index.jsp
我要现在index.jsp 写一段 JavaScript
<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function() //author: meizz
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey)
{
location.href="SessionInvalidate" ;// SessionInvalidate 是一个处理 session 失效的Servlet
}
}
</script>
</head>
<body>
<form action="LoginAction" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="sunmit">
</form>
</body>
</html>
LoginAction --------------------------------------------------------------------
package com.dkp.servlet;
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;
import com.dkp.bean.Users;
import com.dkp.dao.UsersDao;
import com.dkp.dao.impl.UsersDaoImpl;
public class LoginAction extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");// 获得username
String password = request.getParameter("password");// 获得password
UsersDao usersDao = new UsersDaoImpl(); // 创建对象 这是我的验证用户的dao 和他的IMpl(实现)
Users user = usersDao.findUserByUsername(username); // 根据用户名返回用户
if (user != null && user.getPassword().equals(password)) { // 判断返回的用户是不是为空,并且用户的密码和输入的密码是不是一致
// 如果用户不为空,并且密码一样
HttpSession session = request.getSession();
//把用户放进session
session.setAttribute("users", user);
response.sendRedirect("index.jsp");
return;
} else {
response.sendRedirect("index.jsp");
}
}
}
SessionInvalidate-------------------------------------------
package com.dkp.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dkp.action.OnlineUser;
public class SessionInvalidate extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -2799996612094958606L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("users");
session.invalidate();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
----------------------OnlineUser -----------这个类是最主要的 onlineUser 是一个session的监听器
package com.dkp.action;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.dkp.bean.Users;
public class OnlineUser implements HttpSessionListener,
HttpSessionAttributeListener {
private static int activeSessions = 0;
private static Map<String,Users> online=new HashMap<String,Users>();
public static Map<String,Users> getOnline() {
return online;
}
public static int getActiveSessions() {
return activeSessions;
}
public void attributeAdded(HttpSessionBindingEvent event) {
if (event.getName().equals("users")) {
System.out.println("User logged in.");
Users users = (Users) event.getSession().getAttribute("user");
online.put(event.getSession().getId(),users);
}
}
public synchronized void attributeRemoved(HttpSessionBindingEvent event) {
if (event.getName().equals("users")) {
System.out.println("User logged out.");
online.remove(event.getSession().getId());
}
}
public void attributeReplaced(HttpSessionBindingEvent event) {
}
public void sessionCreated(HttpSessionEvent event) {
activeSessions++;
System.out.println("有人来了");
}
public void sessionDestroyed(HttpSessionEvent event) {
if (activeSessions > 0) {
activeSessions--;
System.out.println("有人走了");
}
}
}
-------------------------------------------下面是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<listener>
<listener-class>com.dkp.action.OnlineUser</listener-class>
</listener>
<servlet>
<description>退出</description>
<display-name>退出</display-name>
<servlet-name>SessionInvalidate</servlet-name>
<servlet-class>com.dkp.servlet.SessionInvalidate</servlet-class>
</servlet>
<servlet>
<description>登录</description>
<display-name>登录</display-name>
<servlet-name>LoginAction</servlet-name>
<servlet-class>com.dkp.servlet.LoginAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionInvalidate</servlet-name>
<url-pattern>/SessionInvalidate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginAction</servlet-name>
<url-pattern>/LoginAction</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>admin/index.jsp</welcome-file>
</welcome-file-list>
</web-app>