From 04a929c714b050e52a0e7095cfd53221211bdc2c Mon Sep 17 00:00:00 2001 From: zhai_lw Date: Thu, 10 Jan 2019 19:48:05 +0800 Subject: [PATCH] =?UTF-8?q?1,=E7=99=BB=E5=BD=95=E5=89=8D=E7=AB=AF=E5=92=8C?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/user/Administrator.java | 2 +- src/core/user/utils/AccountManagement.java | 11 +++++++---- src/dao/DBManagement.java | 1 + src/servlet/account/login.java | 20 ++++++++++++++------ src/utils/Utils.java | 9 +++++++++ web/WEB-INF/web.xml | 4 ++++ web/login.jsp | 2 +- 7 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/utils/Utils.java diff --git a/src/core/user/Administrator.java b/src/core/user/Administrator.java index 3901895..49af1c0 100644 --- a/src/core/user/Administrator.java +++ b/src/core/user/Administrator.java @@ -1,4 +1,4 @@ package core.user; -public class Administrator { +public class Administrator extends User { } diff --git a/src/core/user/utils/AccountManagement.java b/src/core/user/utils/AccountManagement.java index cdb726f..e9f934c 100644 --- a/src/core/user/utils/AccountManagement.java +++ b/src/core/user/utils/AccountManagement.java @@ -1,9 +1,11 @@ package core.user.utils; +import core.user.Administrator; import core.user.User; import dao.DBManagement; import error.GExcptAccount; import error.GExcptSQL; +import utils.Utils; import java.lang.reflect.InvocationTargetException; import java.sql.ResultSet; @@ -16,12 +18,12 @@ public interface AccountManagement { "student", "teacher" }; - static User login(String id, String password) throws GExcptSQL, GExcptAccount { + static User login(String id, String password) throws GExcptSQL, GExcptAccount, SQLException { ResultSet rs = null; String sql = null; String userType = null; for(String userTable:userTables){ - sql = "SELECT * FROM "+userTable+"WHERE id="+id; + sql = "SELECT * FROM "+userTable+" WHERE id=\'"+id+"\'"; try { rs = DBManagement.query(sql); } catch (Exception e) { @@ -35,8 +37,9 @@ public interface AccountManagement { if(rs==null){ throw new GExcptAccount("id "+id+"don't exists"); } + rs.next(); try { - if(rs.getString("password")!=password) + if(!rs.getString(2).equals(password)) throw new GExcptAccount("password wrong"); return createUser(userType,rs); } catch (SQLException e) { @@ -56,7 +59,7 @@ public interface AccountManagement { } static User getUser(String userType){ try { - return (User)Class.forName("core.user."+userType).getDeclaredConstructor().newInstance(); + return (Administrator)Class.forName("core.user."+ Utils.toUpperFristChar(userType)).getDeclaredConstructor().newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { diff --git a/src/dao/DBManagement.java b/src/dao/DBManagement.java index 685c654..c31829e 100644 --- a/src/dao/DBManagement.java +++ b/src/dao/DBManagement.java @@ -50,6 +50,7 @@ public class DBManagement { con = DBManagement.getConnection(); Statement stmt = con.createStatement(); rs = stmt.executeQuery(sql); + System.out.println(rs); } catch (SQLException e) { throw new GExcptSQL("Connect Failure"); }finally { diff --git a/src/servlet/account/login.java b/src/servlet/account/login.java index 7bb4417..ef1e238 100644 --- a/src/servlet/account/login.java +++ b/src/servlet/account/login.java @@ -1,6 +1,7 @@ package servlet.account; import java.io.IOException; +import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; @@ -10,6 +11,9 @@ import javax.servlet.http.HttpServletResponse; import core.user.Student; import core.user.User; +import core.user.utils.AccountManagement; +import error.GExcptAccount; +import error.GExcptSQL; @WebServlet("/login") public class login extends HttpServlet { @@ -17,12 +21,16 @@ public class login extends HttpServlet { throws ServletException, IOException { String id=request.getParameter("id"); String password=request.getParameter("password"); - if(id=="201630126050"&&password=="823169880") - { - request.getRequestDispatcher("/service").forward(request,response); - } - else - response.sendRedirect(""); + User user = null; + try { + user = AccountManagement.login(id,password); + } catch (GExcptSQL | SQLException gExcptSQL) { + gExcptSQL.printStackTrace(); + } catch (GExcptAccount gExcptAccount) { + gExcptAccount.printStackTrace(); + } + request.getSession().setAttribute("User",user); + request.getRequestDispatcher("/home.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) diff --git a/src/utils/Utils.java b/src/utils/Utils.java new file mode 100644 index 0000000..1a142d7 --- /dev/null +++ b/src/utils/Utils.java @@ -0,0 +1,9 @@ +package utils; + +public interface Utils { + public static String toUpperFristChar(String string) { + char[] charArray = string.toCharArray(); + charArray[0] -= 32; + return String.valueOf(charArray); + } +} diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index 37a5f89..a39191e 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -24,4 +24,8 @@ default *.js + + default + *.png + \ No newline at end of file diff --git a/web/login.jsp b/web/login.jsp index 92c536f..232ca95 100644 --- a/web/login.jsp +++ b/web/login.jsp @@ -25,7 +25,7 @@
GDMS
-
+