LoginServlet登录页面: package com.tanzhou.servlet; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.Enumeration; 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.tanzhou.util.User; import com.tanzhou.util.UserList; public class LoginServlet extends HttpServlet { public LoginServlet() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); String name = getNewString(request.getParameter("username")); String pass = request.getParameter("userpass"); if(name==null || "".equals(name)){ response.sendRedirect("/OnlineUserDemo/login.html"); return ; }else{ // 讲登录的用户名和密码保存到session中 HttpSession session = request.getSession(true); // 保存对象到session User user = (User)session.getAttribute("user"); if(user==null || !name.equals(user.getName())){ user = new User(name); session.setAttribute("user", user); } } PrintWriter out = response.getWriter(); out.println("欢迎用户:"+name+"登录"+"<br/>"); out.println("<hr>"+"在线人数列表:"+"<br/>"); UserList ul = UserList.getInstance(); /** * Enumeration(列举) */ Enumeration<String> enums = ul.getUserList(); int i = 0; while(enums.hasMoreElements()){ out.println(enums.nextElement()); // 遍历出来的用户信息 out.println(" "); if((++i%8)==0){ out.println("<br>"); } } out.println("<hr>"); out.println("<br>"+"在线人数:"+ul.getUserCount()); out.println("<br>"+"<a href='/OnlineUserDemo/servlet/OutServlet'>退出登录</a>"); out.flush(); out.close(); } public void init() throws ServletException { } /** * 编码集处理 * @param str * @return * @throws UnsupportedEncodingException */ public static String getNewString(String str) throws UnsupportedEncodingException { return new String(str.getBytes("ISO-8859-1"),"UTF-8"); } } OutServlet退出页面: package com.tanzhou.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.tanzhou.util.User; public class OutServlet extends HttpServlet { /** * Constructor of the object. */ public OutServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(true); User user = (User)session.getAttribute("user"); session.invalidate(); // session销毁 response.sendRedirect("/OnlineUserDemo/login.html"); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } } User页面: package com.tanzhou.util; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; /** * * @author Tony Liu * * *如果一个对象实现了HttpSessionBindingListener接口,当这个对象被绑定到session中或者从session中被删除时, *Servlet容器会通知该对象。 */ public class User implements HttpSessionBindingListener{ // 得到UserList的实例 private UserList ul = UserList.getInstance(); private String name; public User(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * session创建对象--自动调用valueBound 用户登录 */ @Override public void valueBound(HttpSessionBindingEvent arg0) { ul.addUser(name); // 添加用户 } /** * session销毁--自动调用valueUnbound 用户退出 */ @Override public void valueUnbound(HttpSessionBindingEvent arg0) { ul.removeUser(name); //删除用户 } } UserList页面: package com.tanzhou.util; import java.util.Enumeration; import java.util.Vector; /** * 用户集合类 * @author Ming * * * * 单例模式---是指一个类只有一个实例,并且能够自行的实例化 * 特点:构造方法是私有 * * 单例模式:懒汉式单例 饿汉式单例 登记式单例 * 特点: * 1、单例类只能有一个实例 * 2、单例类必须自己创建自己的唯一实例 * 3、单例类必须给所有其他对象提供这一实例 */ public class UserList { private static final UserList userList = new UserList(); /** * List 接口一共有三个 ArrayList Vector LinkedList */ private Vector<String> v; //用户集合 private UserList(){ v = new Vector<String>(); } public static UserList getInstance(){ return userList; } /** * 用户登录-添加用户信息 * @param name */ public void addUser(String name){ if(name!=null){ v.addElement(name); } } /** * 用户退出的方法 * @param anme */ public void removeUser(String name){ if(name!=null){ v.remove(name); } } /** * 用户数量的统计 * @return */ public int getUserCount(){ return v.size(); } /** * Enumeration (列举) * @return */ public Enumeration<String> getUserList(){ return v.elements(); } }