servlet+java+jsp實現線上人數的統計

來源:互聯網
上載者:User

大家好,這是我第一次寫部落格 ,寫的不好還請見諒,呵呵

 

我今天一直在弄關於統計線上人數和線上使用者的問題,,在網上找了好多,但是沒有太好的,,多優點差強人意,所以我就自己弄了弄  ,現在我把代碼帖出來,讓大家看看,,。。。。

 

 

 

首先: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("有人走了");
  }
 }
}

 

 

jsp+servlet+java 實現統計線上人數,線上使用者

dev.firnow.com    時間 : 2010-05-17  作者:佚名   編輯:Mr.阿布 點擊:  855

[ 評論 ]

-------------------------------------------下面是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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.