当前位置: 代码迷 >> 综合 >> 模拟在线人数统计和网站的访问量
  详细解决方案

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

热度:30   发布时间:2024-02-07 05:25:48.0

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("<script>alert('login Successs'); window.location='index.jsp' </script>");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("<script>alert('password Failed');window.location='index.jsp' </script>");out.flush();out.close();}}else {PrintWriter out = resp.getWriter();out.print("<script>alert('not exist'); window.location='index.jsp' </script>");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();}
}

判断:

@Overrideprotected 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("<script>alert('success');window.location='index.jsp' </script>");out.flush();out.close();				} else {PrintWriter out = resp.getWriter();out.print("<script>alert('failed');window.location='index.jsp' </script>");out.flush();out.close();				}} catch (SQLException e) {e.printStackTrace();}}if (str3 != null) {logout(req,resp);}	            
}

 web.xml

<servlet><servlet-name>LOGIN</servlet-name><servlet-class>fir.LOGIN</servlet-class></servlet><servlet-mapping><servlet-name>LOGIN</servlet-name><url-pattern>/LOGIN.do</url-pattern></servlet-mapping>

网页


<%@ 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 + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="/logn/">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><h1>在线人数为:${count==null? 0:count}</h1><h1>总访问量:${totalcount}</h1>${users}<form action="Main.do" method="get">     用户名:<input  type="text" name="username">密 码: <input  type="password" name="password"><input type="submit" value="登陆" name="login"><input type="submit" value="注册" name="register"><input type="submit" value="注销" name="logout"></form>
</body>
</html>

 

  相关解决方案