From aa8e91a9e0b7db83397f57067c064be672d51c45 Mon Sep 17 00:00:00 2001 From: xianhuhong <1659909990@qq.com> Date: Sun, 14 Apr 2024 16:26:17 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0maven=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E7=9A=84=E9=A1=B9=E7=9B=AE=E5=92=8C=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 52 ++++++++++++++++++++ src/main/java/servlet/LoginServlet.java | 61 ++++++++++++++++++++++++ src/main/webapp/WEB-INF/web.xml | 7 +++ src/main/webapp/index.jsp | 63 +++++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/servlet/LoginServlet.java create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/index.jsp diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..53b2793 --- /dev/null +++ b/pom.xml @@ -0,0 +1,52 @@ + + + + 4.0.0 + + org.example + homework + 1.0-SNAPSHOT + war + + homework Maven Webapp + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + + mysql + mysql-connector-java + 5.1.38 + + + + + + + + homework + + diff --git a/src/main/java/servlet/LoginServlet.java b/src/main/java/servlet/LoginServlet.java new file mode 100644 index 0000000..83d7d50 --- /dev/null +++ b/src/main/java/servlet/LoginServlet.java @@ -0,0 +1,61 @@ +package servlet; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +@WebServlet("/login") +public class LoginServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + + String username = request.getParameter("username"); + String password = request.getParameter("password"); + + // 数据库连接参数 + String jdbcUrl = "jdbc:mysql://localhost:3306/myhomework?useSSL=false"; + String dbUser = "root"; + String dbPassword = "root"; + + try { + // 加载数据库驱动程序 + Class.forName("com.mysql.jdbc.Driver"); + + // 建立数据库连接 + Connection con = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword); + + // 准备 SQL 查询 + String query = "SELECT * FROM user WHERE username=? AND password=?"; + PreparedStatement ps = con.prepareStatement(query); + ps.setString(1, username); + ps.setString(2, password); + + // 执行查询 + ResultSet rs = ps.executeQuery(); + + if (rs.next()) { + // 用户名和密码匹配 + out.println("

Login successful!

"); + } else { + // 用户名或密码错误 + out.println("

Invalid username or password!

"); + } + + // 关闭连接 + con.close(); + } catch (Exception e) { + out.println("

Error: " + e.getMessage() + "

"); + } + } +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..106f8d0 --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,63 @@ + + + + + + Login + + + +
+

Login

+
+ +
+ +
+ +
+
+ + -- Gitee From 35a10893b41937fe28b5c1012536d38426beb490 Mon Sep 17 00:00:00 2001 From: xianhuhong <1659909990@qq.com> Date: Sun, 14 Apr 2024 16:29:24 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0maven=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E7=9A=84=E9=A1=B9=E7=9B=AE=E5=92=8C=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 +++ .idea/artifacts/web_framework_war.xml | 14 +++++ .../artifacts/web_framework_war_exploded.xml | 26 ++++++++ .idea/compiler.xml | 16 +++++ .idea/dataSources.xml | 12 ++++ .idea/encodings.xml | 7 +++ .idea/jarRepositories.xml | 20 ++++++ ..._javax_servlet_javax_servlet_api_3_1_0.xml | 13 ++++ .idea/libraries/Maven__junit_junit_4_11.xml | 13 ++++ ...ven__mysql_mysql_connector_java_5_1_38.xml | 13 ++++ .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 ++++ .idea/misc.xml | 19 ++++++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .idea/web-framework.iml | 29 +++++++++ target/homework/META-INF/MANIFEST.MF | 5 ++ target/homework/WEB-INF/web.xml | 7 +++ target/homework/index.jsp | 63 +++++++++++++++++++ 18 files changed, 292 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/artifacts/web_framework_war.xml create mode 100644 .idea/artifacts/web_framework_war_exploded.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/dataSources.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries/Maven__javax_servlet_javax_servlet_api_3_1_0.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_11.xml create mode 100644 .idea/libraries/Maven__mysql_mysql_connector_java_5_1_38.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/web-framework.iml create mode 100644 target/homework/META-INF/MANIFEST.MF create mode 100644 target/homework/WEB-INF/web.xml create mode 100644 target/homework/index.jsp diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/artifacts/web_framework_war.xml b/.idea/artifacts/web_framework_war.xml new file mode 100644 index 0000000..4804426 --- /dev/null +++ b/.idea/artifacts/web_framework_war.xml @@ -0,0 +1,14 @@ + + + $PROJECT_DIR$/target + + + web-framework + war + + + + + + + \ No newline at end of file diff --git a/.idea/artifacts/web_framework_war_exploded.xml b/.idea/artifacts/web_framework_war_exploded.xml new file mode 100644 index 0000000..7c1aab7 --- /dev/null +++ b/.idea/artifacts/web_framework_war_exploded.xml @@ -0,0 +1,26 @@ + + + $PROJECT_DIR$/target/homework + + + true + web-framework + war + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..159869d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..ba0db35 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..f86397b --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__javax_servlet_javax_servlet_api_3_1_0.xml b/.idea/libraries/Maven__javax_servlet_javax_servlet_api_3_1_0.xml new file mode 100644 index 0000000..925cf77 --- /dev/null +++ b/.idea/libraries/Maven__javax_servlet_javax_servlet_api_3_1_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_11.xml b/.idea/libraries/Maven__junit_junit_4_11.xml new file mode 100644 index 0000000..d0ef22c --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__mysql_mysql_connector_java_5_1_38.xml b/.idea/libraries/Maven__mysql_mysql_connector_java_5_1_38.xml new file mode 100644 index 0000000..287f3d0 --- /dev/null +++ b/.idea/libraries/Maven__mysql_mysql_connector_java_5_1_38.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..7957e46 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..40e66dc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8e99870 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/web-framework.iml b/.idea/web-framework.iml new file mode 100644 index 0000000..81dc73a --- /dev/null +++ b/.idea/web-framework.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/target/homework/META-INF/MANIFEST.MF b/target/homework/META-INF/MANIFEST.MF new file mode 100644 index 0000000..26075f9 --- /dev/null +++ b/target/homework/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Created-By: IntelliJ IDEA +Built-By: 16599 +Build-Jdk: version 17.0.10 + diff --git a/target/homework/WEB-INF/web.xml b/target/homework/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/target/homework/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/target/homework/index.jsp b/target/homework/index.jsp new file mode 100644 index 0000000..106f8d0 --- /dev/null +++ b/target/homework/index.jsp @@ -0,0 +1,63 @@ + + + + + + Login + + + +
+

Login

+
+ +
+ +
+ +
+
+ + -- Gitee From 882e027bc640e0d3d0163922500083178473150d Mon Sep 17 00:00:00 2001 From: xianhuhong <1659909990@qq.com> Date: Sun, 14 Apr 2024 16:52:59 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=9A=84=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/servlet/LoginServlet.java | 10 +++++-- src/main/webapp/index.jsp | 5 ++++ src/main/webapp/loginSuccess.jsp | 35 +++++++++++++++++++++++++ target/homework/index.jsp | 5 ++++ target/homework/loginSuccess.jsp | 35 +++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/main/webapp/loginSuccess.jsp create mode 100644 target/homework/loginSuccess.jsp diff --git a/src/main/java/servlet/LoginServlet.java b/src/main/java/servlet/LoginServlet.java index 83d7d50..b207b90 100644 --- a/src/main/java/servlet/LoginServlet.java +++ b/src/main/java/servlet/LoginServlet.java @@ -46,10 +46,16 @@ public class LoginServlet extends HttpServlet { if (rs.next()) { // 用户名和密码匹配 - out.println("

Login successful!

"); +// out.println("

Login successful!

"); + + // 使用重定向跳转到登录成功页面 + response.sendRedirect("loginSuccess.jsp?username=" + username); } else { // 用户名或密码错误 - out.println("

Invalid username or password!

"); +// out.println("

Invalid username or password!

"); + // 使用请求转发重新加载 index.jsp 页面,并显示错误消息 + request.setAttribute("error", "true"); + request.getRequestDispatcher("index.jsp").forward(request, response); } // 关闭连接 diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 106f8d0..df87534 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -51,6 +51,11 @@

Login

+ + <% if ("true".equals(request.getAttribute("error"))) { %> +

Invalid username or password!

+ <% } %> +

diff --git a/src/main/webapp/loginSuccess.jsp b/src/main/webapp/loginSuccess.jsp new file mode 100644 index 0000000..623c389 --- /dev/null +++ b/src/main/webapp/loginSuccess.jsp @@ -0,0 +1,35 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + Login Success + + + +
+

Welcome, <%= request.getParameter("username") %>!

+
+ + diff --git a/target/homework/index.jsp b/target/homework/index.jsp index 106f8d0..df87534 100644 --- a/target/homework/index.jsp +++ b/target/homework/index.jsp @@ -51,6 +51,11 @@
diff --git a/src/main/webapp/loginSuccess.jsp b/src/main/webapp/loginSuccess.jsp index 623c389..bf83b18 100644 --- a/src/main/webapp/loginSuccess.jsp +++ b/src/main/webapp/loginSuccess.jsp @@ -1,35 +1,28 @@ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<%@ page import="java.util.Set" %> - + - Login Success - + + 在线用户列表 -
-

Welcome, <%= request.getParameter("username") %>!

-
+<% + Set onlineUsers = (Set) application.getAttribute("onlineUsers"); + int totalUsers = onlineUsers.size(); +%> +

当前在线用户总数: <%= totalUsers %>

+

当前在线用户:

+
    + <% + for (String user : onlineUsers) { + %> +
  • <%= user %>
  • + <% } %> +
+ +
+ +
diff --git a/target/homework/WEB-INF/web.xml b/target/homework/WEB-INF/web.xml index 9f88c1f..7dd1ef7 100644 --- a/target/homework/WEB-INF/web.xml +++ b/target/homework/WEB-INF/web.xml @@ -2,6 +2,14 @@ "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > + + Archetype Created Web Application + + + + listener.SessionListener + + diff --git a/target/homework/loginSuccess.jsp b/target/homework/loginSuccess.jsp index 623c389..bf83b18 100644 --- a/target/homework/loginSuccess.jsp +++ b/target/homework/loginSuccess.jsp @@ -1,35 +1,28 @@ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<%@ page import="java.util.Set" %> - + - Login Success - + + 在线用户列表 -
-

Welcome, <%= request.getParameter("username") %>!

-
+<% + Set onlineUsers = (Set) application.getAttribute("onlineUsers"); + int totalUsers = onlineUsers.size(); +%> +

当前在线用户总数: <%= totalUsers %>

+

当前在线用户:

+
    + <% + for (String user : onlineUsers) { + %> +
  • <%= user %>
  • + <% } %> +
+ +
+ +
-- Gitee From 1b55fd722de2c7d3f7f3bfbe943a3358e07097c0 Mon Sep 17 00:00:00 2001 From: xianhuhong <1659909990@qq.com> Date: Tue, 23 Apr 2024 10:15:34 +0800 Subject: [PATCH 14/14] =?UTF-8?q?=E5=9F=BA=E4=BA=8EListener=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=9C=A8=E7=BA=BF=E4=BA=BA=E6=95=B0=E5=92=8C=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=94=A8=E6=88=B7=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- target/homework/index.jsp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/target/homework/index.jsp b/target/homework/index.jsp index df87534..910aff3 100644 --- a/target/homework/index.jsp +++ b/target/homework/index.jsp @@ -42,10 +42,25 @@ border: none; border-radius: 4px; cursor: pointer; + margin-top: 10px; /* Add margin to separate button from inputs */ } .login-container button:hover { background-color: #0056b3; } + /* Style for register button */ + .register-button { + width: 100%; + padding: 10px; + background-color: #28a745; + color: #fff; + border: none; + border-radius: 4px; + cursor: pointer; + margin-top: 10px; /* Add margin to separate button from inputs */ + } + .register-button:hover { + background-color: #218838; + } @@ -63,6 +78,12 @@
+ + +
+ +
+
-- Gitee