diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..d99a07ce5c6c35c1b250b8ea4f01a80e0e4ee44a --- /dev/null +++ b/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + org.example + web_tes + war + 1.0-SNAPSHOT + web_tes Maven Webapp + http://maven.apache.org + + + + + junit + junit + 3.8.1 + test + + + + + + mysql + mysql-connector-java + 5.1.48 + + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + junit + junit + 4.13.2 + test + + + + + web_tes + + diff --git a/src/main/java/net/xyx/bean/User.java b/src/main/java/net/xyx/bean/User.java new file mode 100644 index 0000000000000000000000000000000000000000..a5e67f9f08a4575eb022a6935cfae37dbcad2f10 --- /dev/null +++ b/src/main/java/net/xyx/bean/User.java @@ -0,0 +1,64 @@ +package net.xyx.bean; + +import java.util.Date; + + +public class User { + private int id; + private String username; + private String password; + private String telephone; + private Date registerTime; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getTelephone() { + return telephone; + } + + public void setTelephone(String telephone) { + this.telephone = telephone; + } + + public Date getRegisterTime() { + return registerTime; + } + + public void setRegisterTime(Date registerTime) { + this.registerTime = registerTime; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + ", telephone='" + telephone + '\'' + + ", registerTime=" + registerTime + + '}'; + } +} + diff --git a/src/main/java/net/xyx/dao/UserDao.java b/src/main/java/net/xyx/dao/UserDao.java new file mode 100644 index 0000000000000000000000000000000000000000..5e0df9bc1a66e81b9c64a65b223a13564bdb056a --- /dev/null +++ b/src/main/java/net/xyx/dao/UserDao.java @@ -0,0 +1,51 @@ +package net.xyx.dao; + +import net.xyx.bean.User; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import net.xyx.dbutils.ConnectionManager; + +public class UserDao { + + + public User login(String username, String password) { + // 声明用户对象 + User user = null; + + // 获取数据库连接 + Connection conn = ConnectionManager.getConnection(); + try { + // 定义SQL字符串 + String strSQL = "SELECT * FROM t_user WHERE username = ? AND password = ?"; + // 创建预备语句对象 + PreparedStatement pstmt = conn.prepareStatement(strSQL); + // 设置占位符 + pstmt.setString(1, username); + pstmt.setString(2, password); + // 执行查询,返回结果集 + ResultSet rs = pstmt.executeQuery(); + // 判断结果集是否为空 + if (rs.next()) { + // 创建用户对象 + user = new User(); + // 利用当前记录字段值来设置用户对象属性 + user.setId(rs.getInt("id")); + user.setUsername(rs.getString("username")); + user.setPassword(rs.getString("password")); + user.setTelephone(rs.getString("telephone")); + user.setRegisterTime(rs.getTimestamp("register_time")); + } + } catch (SQLException e) { + System.err.println(e.getMessage()); + } finally { + // 关闭数据库连接 + ConnectionManager.closeConnection(conn); + } + + // 返回用户对象 + return user; + } +} + diff --git a/src/main/java/net/xyx/dbutils/ConnectionManager.java b/src/main/java/net/xyx/dbutils/ConnectionManager.java new file mode 100644 index 0000000000000000000000000000000000000000..2475a6a7665a63d87f20cf4c5016200959ec6c53 --- /dev/null +++ b/src/main/java/net/xyx/dbutils/ConnectionManager.java @@ -0,0 +1,73 @@ +package net.xyx.dbutils; + +import javax.swing.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + + +public class ConnectionManager { + private static final String DRIVER = "com.mysql.jdbc.Driver"; // 数据库驱动程序 + private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false"; // 数据库统一资源标识符 + private static final String USER = "root"; // 数据库用户 + private static final String PASSWORD = "root"; // 数据库密码 + + //私有化构造方法,拒绝实例化 + private ConnectionManager() { + } + + /** + * 获取数据库连接静态方法 + * + * @return 数据库连接对象 + */ + public static Connection getConnection() { + // 定义数据库连接 + Connection conn = null; + + try { + // 安装数据库驱动程序 + Class.forName(DRIVER); + // 获取数据库连接 + conn = DriverManager.getConnection(URL, USER, PASSWORD); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } + + // 返回数据库连接 + return conn; + } + + + public static void closeConnection(Connection conn) { + // 判断数据库连接是否非空 + if (conn != null) { + try { + // 判断连接是否未关闭 + if (!conn.isClosed()) { + // 关闭数据库连接 + conn.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + + public static void main(String[] args) { + // 获取数据库连接 + Connection conn = getConnection(); + // 判断数据库连接是否成功 + if (conn != null) { + JOptionPane.showMessageDialog(null, "恭喜,数据库连接成功!"); + } else { + JOptionPane.showMessageDialog(null, "遗憾,数据库连接失败!"); + System.out.println("连接成功"); + } + // 关闭数据库连接 + closeConnection(conn); + } +} diff --git a/src/main/java/net/xyx/servlet/LoginServlet.java b/src/main/java/net/xyx/servlet/LoginServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..5322e75bf0d1e019b58defc0e366f05a2b11e6c7 --- /dev/null +++ b/src/main/java/net/xyx/servlet/LoginServlet.java @@ -0,0 +1,54 @@ +package net.xyx.servlet; + +import net.xyx.dao.UserDao; + +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.net.URLEncoder; + +@WebServlet(name = "LoginServlet", urlPatterns = "/login") +public class LoginServlet extends HttpServlet { + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + // 设置请求对象字符编码格式 + request.setCharacterEncoding("utf-8"); + // 获取登录表单数据 + String username = request.getParameter("username"); + String password = request.getParameter("password"); + // 判断登录是否成功 + UserDao userDao=new UserDao(); + userDao.login(username,password); + System.out.println("asdasdas"); + System.out.println("11222"); + System.out.println("rebase"); + if ( userDao.login(username,password)!=null) { + // 采用重定向,跳转到登录成功页面 + response.sendRedirect("success.jsp?username=" + URLEncoder.encode(username, "utf-8")); + } else { + // 采用重定向,跳转到登录失败页面 + response.sendRedirect("failure.jsp?username=" + URLEncoder.encode(username, "utf-8")); + } + System.out.println("rebase"); + System.out.println("base"); + System.out.println("base"); + System.out.println("base"); + } + + /** + * + * + * @param request + * @param response + * @throws ServletException + * @throws IOException + */ + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doPost(request, response); + System.out.println("pick"); + } +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..9f88c1f9632445500e3b3688fe477b860f77d8f2 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/src/main/webapp/failure.jsp b/src/main/webapp/failure.jsp new file mode 100644 index 0000000000000000000000000000000000000000..a70c7d310c35bf3480f5e44191a344416dac9dff --- /dev/null +++ b/src/main/webapp/failure.jsp @@ -0,0 +1,9 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 登录失败 + + +

遗憾,<%=request.getParameter("username")%>,登录失败!

+ + diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..b3b031ec7216ac6958722de63a7a05ef21dc949e --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,32 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 用户登录 + + +

用户登录

+
+ + + + + + + + + + + + + +
账号
密码
+ + +
+
+ + + diff --git a/src/main/webapp/success.jsp b/src/main/webapp/success.jsp new file mode 100644 index 0000000000000000000000000000000000000000..5ce3e772a07b7675055c7aa50433745b653fc616 --- /dev/null +++ b/src/main/webapp/success.jsp @@ -0,0 +1,9 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 登录成功 + + +

恭喜,<%=request.getParameter("username")%>,登录成功!

+ + diff --git a/src/test/java/TestJava.java b/src/test/java/TestJava.java new file mode 100644 index 0000000000000000000000000000000000000000..55e627daa13da138291174b77f837c6826559123 --- /dev/null +++ b/src/test/java/TestJava.java @@ -0,0 +1,36 @@ +import net.xyx.bean.User; +import net.xyx.dao.UserDao; +import org.junit.Test; + + + + +public class TestJava { + UserDao userDao = new UserDao(); + @Test + public void testLogin2(){ + String username = "root"; + String password = "12345"; + // 调用登录方法,返回用户对象 + User user = userDao.login(username, password); + // 判断用户登录是否成功 + if (user != null) { // 成功 + System.out.println("恭喜,用户[" + username + "]登录成功~"); + } else { // 失败 + System.out.println("遗憾,用户[" + username + "]登录失败~"); + } + } + @Test + public void testLogin() { + String username = "root"; + String password = "12345"; + // 调用登录方法,返回用户对象 + User user = userDao.login(username, password); + // 判断用户登录是否成功 + if (user != null) { // 成功 + System.out.println("恭喜,用户[" + username + "]登录成功~"); + } else { // 失败 + System.out.println("遗憾,用户[" + username + "]登录失败~"); + } + } +} diff --git a/target/web_tes/META-INF/MANIFEST.MF b/target/web_tes/META-INF/MANIFEST.MF new file mode 100644 index 0000000000000000000000000000000000000000..aa2c77d9c94c186a154337114577356119993b3c --- /dev/null +++ b/target/web_tes/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Created-By: IntelliJ IDEA +Built-By: 98193 +Build-Jdk: Oracle OpenJDK version 1.8.0_381 + diff --git a/target/web_tes/WEB-INF/web.xml b/target/web_tes/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..9f88c1f9632445500e3b3688fe477b860f77d8f2 --- /dev/null +++ b/target/web_tes/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/target/web_tes/failure.jsp b/target/web_tes/failure.jsp new file mode 100644 index 0000000000000000000000000000000000000000..a70c7d310c35bf3480f5e44191a344416dac9dff --- /dev/null +++ b/target/web_tes/failure.jsp @@ -0,0 +1,9 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 登录失败 + + +

遗憾,<%=request.getParameter("username")%>,登录失败!

+ + diff --git a/target/web_tes/index.jsp b/target/web_tes/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..b3b031ec7216ac6958722de63a7a05ef21dc949e --- /dev/null +++ b/target/web_tes/index.jsp @@ -0,0 +1,32 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 用户登录 + + +

用户登录

+
+ + + + + + + + + + + + + +
账号
密码
+ + +
+
+ + + diff --git a/target/web_tes/success.jsp b/target/web_tes/success.jsp new file mode 100644 index 0000000000000000000000000000000000000000..5ce3e772a07b7675055c7aa50433745b653fc616 --- /dev/null +++ b/target/web_tes/success.jsp @@ -0,0 +1,9 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 登录成功 + + +

恭喜,<%=request.getParameter("username")%>,登录成功!

+ + diff --git a/web_tes.iml b/web_tes.iml new file mode 100644 index 0000000000000000000000000000000000000000..097da30fff058e3e30118049addca49c367d0170 --- /dev/null +++ b/web_tes.iml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file