模拟在线人数统计和网站的访问量

1.模拟在线人数统计和网站的访问量
2.模拟注册和登陆的功能的实现。
注册,相当于往表中插入数据;登陆,相当于要查询数据和前端页面的数据进行匹配

更新数据

//更新数据
public void UpDate(ServletContext sc) {
	Properties pro = new Properties(); 
	String n;
	String filePath = "/Users/ruirui/Desktop/count.txt";
	InputStream in = null;
	try {
		in = new  FileInputStream(filePath);
		pro.load(in);
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}	
	n = pro.getProperty("count");
	int a = Integer.parseInt(n) + 1;
	sc.setAttribute("totalcount", a);
	pro.setProperty("count", a + "");
	try {
		OutputStream os = new FileOutputStream(filePath);
		pro.store(os, null);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

统计在线人数

//在线人数统计
public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
		String username=req.getParameter("username");
	    String password = req.getParameter("password");
	    TB7 tb7 = mapper.query(username);
	    if (tb7 != null) {
	    	if (tb7.getPassword().equals(password)) {
	    		resp.setCharacterEncoding("utf-8");
				PrintWriter out = resp.getWriter();
				out.print("");
				out.flush();
				out.close();								        
	            req.getSession().setAttribute("username", username);
	            //将用户名保存到set集合中
	            names.add(username);
	            //再将names集合保存到application内置对象中
	            req.getServletContext().setAttribute("users", names);              
	            //集合大小即为人数多少
	            req.getServletContext().setAttribute("count", names.size());
	    	}else {
				PrintWriter out = resp.getWriter();
				out.print("");
				out.flush();
				out.close();
	    	}
	    }else {
	    	PrintWriter out = resp.getWriter();
			out.print("");
			out.flush();
			out.close();								
	    } 
	    }

 插入数据

//连接数据库
static {
		try {
			String resource = "mybatis-config.xml";		
			InputStream is = Resources.getResourceAsStream(resource);		
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);				
			SqlSession session = sqlSessionFactory.openSession();
			mapper = session.getMapper(TB7mapper.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
//查看是否存在
private boolean IsExist(String username, String password) throws SQLException, IOException{
		TB7 tb7 = mapper.query(username);
		if (tb7 == null) {
			return false;
		}else {
			return true;
		}
		}
//插入数据
private void register(String username, String password) throws SQLException {		
		try {
			mapper.Register(username, password);		
		}catch (Exception e) {
			e.printStackTrace();
		}
}

判断:

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        req.setCharacterEncoding("utf-8");
        this.UpDate(sc);
        String str1 = req.getParameter("login");
        String str2 = req.getParameter("register");
        String str3 = req.getParameter("logout");
        if (str1 != null) {
        	try {
				login(req,resp);
			} catch (ServletException | IOException | SQLException e) {
				e.printStackTrace();
			}
        }
        if (str2 != null) {
        	String username = req.getParameter("username");
    		String password = req.getParameter("password");
    		System.out.println(username+password);
    		try {
				if (!Isexist(username, password)) {
					this.register(username, password);
					PrintWriter out = resp.getWriter();
					out.print("");
					out.flush();
					out.close();				
				} else {
					PrintWriter out = resp.getWriter();
					out.print("");
					out.flush();
					out.close();				
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
        }
        if (str3 != null) {
        	logout(req,resp);
        }	            
}

 web.xml


    LOGIN
    fir.LOGIN
  
  
    LOGIN
    /LOGIN.do
  

网页


<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+request.getServerName() + ":"
 + request.getServerPort() + path + "/";
%>





Insert title here


 

在线人数为:${count==null? 0:count}

总访问量:${totalcount}

${users}
用户名: 密 码:

 

你可能感兴趣的:(mysql)