在线人员统计

package 在线人员统计;

import javax.servlet.*;

import java.io.*;
import javax.servlet.http.*;

import java.util.Enumeration;
/*
 * 用户登录
 */
public class OnlineUser extends HttpServlet{
   public void doGet(HttpServletRequest req,HttpServletResponse resp)
   			throws ServletException ,IOException
   {
	   req.setCharacterEncoding("gb2312");
	   String name=req.getParameter("user");
	   String pwd=req.getParameter("password");
	   if(null==name||null==pwd||name.equals("")||name.equals("")||pwd.equals("")){
		  
	   }else{
		   HttpSession session=req.getSession();
		   User user=(User)session.getAttribute("user");
		   if(null==user||!name.equals(user.getName())){
			   user=new User(name);
			   session.setAttribute("user",user);
		   }
	   
		   resp.setContentType("text/html;charset=gb2312");
		   PrintWriter out=resp.getWriter();
		   out.println("欢迎用户<b>"+name+"</b>登陆");
		   UserList ul=UserList.getInstance();
		   out.println("<br>当前在线的用户列表:<br>");
		   Enumeration<String> enums=ul.getUserList();
		   int i=0;
		   while(enums.hasMoreElements()){
			   out.println(enums.nextElement());
			   out.println("&nbsp;&nbsp:&nbsp;&nbsp;");
			   if(++i==10){
				   out.println("<br>");
			   }
		   }
		   out.println("<br>当前在线的用户数:"+i);
		   out.println("<p><a href=logout>退出登录</a>");
		   out.close();
	   }
   }
   public void doPost(HttpServletRequest req,HttpServletResponse resp)
		throws ServletException ,IOException{
	   		doGet(req,resp);
   }
}


package 在线人员统计;

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;
/*
 * 用户退出
 */
public class LogoutServlet extends HttpServlet{
	public void doGet(HttpServletRequest req,HttpServletResponse resp)
		throws ServletException ,IOException{
		resp.setContentType("text/html;charset=gb2312");
		HttpSession session=req.getSession();
		User user=(User)session.getAttribute("user");
		session.invalidate();
		PrintWriter out=resp.getWriter();
		out.println("<html><head><title>退出登录</title></head><body>");
		out.println(user.getName()+",你已退出登录<br>");
		//out.println("<a href=online>重新登录");
		out.println("</body></html>");
		out.close();
		
	}

}




package 在线人员统计;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class User implements HttpSessionBindingListener{
	private String name;
	private UserList ul=UserList.getInstance();
	public User(){}
	public User(String name){
		this.name=name;
		
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
	public void valueBound(HttpSessionBindingEvent event){
		ul.addUser(name);
	}
	public void valueUnbound(HttpSessionBindingEvent event){
		ul.removeUser(name);
	}
	

}



package 在线人员统计;
import java.util.Vector;
import java.util.Enumeration;
public class UserList {
	private static final UserList userList=new UserList();
	private Vector<String> v;
	private UserList(){
		v=new Vector<String>();
		
	}
	public static UserList getInstance(){
		return userList;
	}
	public void addUser(String name){
		if(name!=null)
			v.addElement(name);
	} 
	public void removeUser(String name){
		if(name!=null)
			v.remove(name);
	}
	public Enumeration<String> getUserList(){
		return v.elements();
	}
	public int getUserCount(){
		return v.size();
	}
	
}

/*
web.xml
*/
  <!-- 在线人员统计 -->
  <servlet>
  	<servlet-name>OnlineUser</servlet-name>
  	<servlet-class>在线人员统计.OnlineUser</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>OnlineUser</servlet-name>
  	<url-pattern>/online</url-pattern>
  </servlet-mapping>
  
    <servlet>
  	<servlet-name>LogoutServlet</servlet-name>
  	<servlet-class>在线人员统计.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>LogoutServlet</servlet-name>
  	<url-pattern>/logout</url-pattern>
  </servlet-mapping>
  <!-- 在线人员统计 -->





<%@ page language="java" pageEncoding="gb2312" import="java.util.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="online" method="post">
    	<table>
    		<tr>
    			<td>
    				请输入用户名:
    			</td>
    			<td><input type="text" name="user"></td>
    			<td>
    				密码:
    			</td>
    			<td><input type="password" name="password"></td>
    		</tr>
    		<tr>
    		  <td><input type="reset" value="重填"></td>
    		  <td><input type="submit" value="登录"></td>
    		</tr>
    	</table>
    </form>
  </body>
</html>




你可能感兴趣的:(java,html,Web,jsp,servlet)