在线用户显示,防止重复登录

1.lifecycle : 生命周期
listener : 监听器
(1)session 监听:能监听用户的到来,也能监听用户的离开
session:从用户连接到网站,到用户离开网站,这期间叫做一个session(会话)
半小时后session过期:
(2)request 监听:请求监听,监听请求的开始和结束
(3)application(servlet context)监听:监听服务器的启动和服务器的停止

2.application启动时,在application中存入一个名为online的HashMap
用户来了,我要把这个用户存在在线用户表中
在线用户表需要存在数据库中吗??  
--不太需要
--如果要存的话,存在session中,或者存在application中
只有存在application中的数据,才能被所有访问网站的用户看到
而存在session中的数据,只有被当前用户看到
HashMap(Map存的是键值对):sessionid -- key    User(...) -- value

3.sessionCreated方法中,HashMap中存放一个用户

4,simpledateformat 格式化时间


页面:Online.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="javastudy.*" import="java.util.*" %>




Insert title here


<%
@SuppressWarnings("unchecked")
	HashMap map = (HashMap) application.getAttribute("ONLINEUSERS");
 	Set ids = map.keySet();//所有键值的集合
	Iterator it = ids.iterator();
	int i = 1;
	while (it.hasNext()) {
		User user = map.get(it.next());
		out.println(i + ": " + "来访时间:" + user.getvTime() +"   IP:" + user.getIp());
		i++;
	} 
%>

用户bean:User.java
package javastudy;

import java.util.Date;

public class User {
	private int id;
	private String name;
	private String pwd;
	private String ip;
	private Date  vTime; //年月日时分秒  java.sql.Date 只有年月日
	private Date lTime;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public Date getvTime() {
		return vTime;
	}
	public void setvTime(Date vTime) {
		this.vTime = vTime;
	}
	public Date getlTime() {
		return lTime;
	}
	public void setlTime(Date lTime) {
		this.lTime = lTime;
	}
}

监听器:MyListner.java

package javastudy;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

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.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class MyListener
 *
 */
@WebListener
public class MyListener implements HttpSessionListener, 
 ServletContextListener ,ServletRequestListener {
	HttpServletRequest request;
    /**
     * Default constructor. 
     */
    public MyListener() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO 服务器关闭
		
	}

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		// TODO 服务器启动,网站启动
		//网站启动时,做一个空的HashMap,网站启动时,一旦有用户到来,就存放在该map中
		HashMap map = new HashMap();
		servletContextEvent.getServletContext().setAttribute("ONLINEUSERS", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void sessionCreated(HttpSessionEvent httpSessionEvent) {
		// TODO 记录用户到来
		User user = new User();
		user.setvTime(new Date());//设置来的时间
		//需要request才能得到用户客户端的信息
		user.setIp(request.getRemoteAddr());//通过request获取ip地址
		//存到map中
		HashMap map = (HashMap) 
				request.getServletContext().getAttribute("ONLINEUSERS");
		map.put(httpSessionEvent.getSession().getId(), user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
		// TODO 用户离开
		HashMap map = (HashMap) 
				request.getServletContext().getAttribute("ONLINEUSERS");
		Set ids = map.keySet();//所有键值的集合
		Iterator it = ids.iterator();
		while (it.hasNext()) {
			String key = it.next();
			if (key.equals(httpSessionEvent.getSession().getId())) {
				map.remove(key);
				break;
			}
		}
	}

	@Override
	public void requestDestroyed(ServletRequestEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void requestInitialized(ServletRequestEvent arg0) {
		// TODO Auto-generated method stub
		request = (HttpServletRequest) arg0.getServletRequest();
	}
	
}


结果显示:



你可能感兴趣的:(JavaWeb)