登录Servlet
package web;

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 bean.User;

public class LoginServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//将值传进去;
		User user=new User();
		String id=req.getParameter("id");
		user.setId(id);
		user.setUsername(req.getParameter("username"));
		
		HttpSession session=req.getSession();
		session.setAttribute("user",user);
		session.setAttribute(id,id);
		
		System.out.println(((User)session.getAttribute("user")).getId());
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}


监听器:

package listener;

import java.util.*;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import bean.User;

public class OnLineUserListener implements ServletContextListener,
		HttpSessionListener, HttpSessionAttributeListener {

		//私有属性;
	private ServletContext application;
	
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	/*web服务器启动开始监听
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent se) {
		//应用启动时,创建一个set,保存所有在线用户;
		Set<User>onLineUsers=new HashSet<User>();
		//将set添加到application范围中去
		application=se.getServletContext();
		application.setAttribute("onLineUsers",onLineUsers);
	}

	public void sessionCreated(HttpSessionEvent arg0) {

	}

	public void sessionDestroyed(HttpSessionEvent se) {
		//用户注销后,得到用户注销的信息;
		HttpSession session=se.getSession();
		//得到注销的用户信息;
		User logoutUser=(User)session.getAttribute("user");
		
		//从application中取出在线用户列表;
		Set<User>onLineUsers=(Set)application.getAttribute("onLineUsers");
		//把注销用户从列中删除;
		onLineUsers.remove(logoutUser); 
	}

	/*用户登录后
	 * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
	 */
	public void attributeAdded(HttpSessionBindingEvent se) {
		//用户登录成功后
		System.out.println("增加属性:");
		HttpSession session=se.getSession();
		//得到新登录用户信息
		User newUser=(User)session.getAttribute("user");
		System.out.println(newUser.getUsername());
		//从application中取出在线用户列表;
		Set<User>onLineUsers=(Set)application.getAttribute("onLineUsers");
		//将新用户添加到列中去
		System.out.println("o"+onLineUsers);
		onLineUsers.add(newUser);
	}

	public void attributeRemoved(HttpSessionBindingEvent se) {
		//用户注销后,得到注销后的用户信息;
		User logoutUser=(User)se.getValue();
		//从application中取出在线用户列表;
		Set<User>onLineUsers=(Set)application.getAttribute("onLineUsers");
		//把注销用户从列中删除;
		onLineUsers.remove(logoutUser);
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {

	}

}

页面端;

在线用户:

    <c:forEach items="${onLineUsers}" var="user">
        id:${user.id },name:${user.username }
    </c:forEach>


posted on 2016-11-16 22:19  波波嘟  阅读(357)  评论(0编辑  收藏  举报