(问答题)
一.模拟在线人数统计和网站的访问量 2.模拟注册和登陆的功能的实现 注册 相当于 往表中插入数据 登陆 相当于要查询数据和前端页面的数据进行匹配
1.模拟在线人数统计和网站访问量
(1).这个我们可以利用本地文件来存储网站的总访问量,网站每登陆一次就让文件储存的数值加一。然后利用setAttribute()方法去给我们需要展示在页面的总访问量赋值和名字,类似于Map。这个更新数据可以单独一个方法:
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();
}
}
(2).统计在线人数
每登陆一位用户,在线人数就会加一,同样我们用setAttribute()去储存数据。当用户注销的时候,在线人数会减去一。
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();
}
}
(3).我们利用mybatis去连接数据库和进行对数据库的操作:
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();
}
}
(4).注册方法
每次注册前先找在我们的数据库中查找我们输入的用户名,如果存在就不会再插入数据,否则向数据库插入数据。
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("<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>Main</servlet-name>
<servlet-class>fir.Main</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/Main.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>
最终效果图:
登陆后: