1,登录前端和后端对接

wlf
zhai_lw 6 years ago
parent 90d6124e61
commit 04a929c714

@ -1,4 +1,4 @@
package core.user; package core.user;
public class Administrator { public class Administrator extends User {
} }

@ -1,9 +1,11 @@
package core.user.utils; package core.user.utils;
import core.user.Administrator;
import core.user.User; import core.user.User;
import dao.DBManagement; import dao.DBManagement;
import error.GExcptAccount; import error.GExcptAccount;
import error.GExcptSQL; import error.GExcptSQL;
import utils.Utils;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -16,12 +18,12 @@ public interface AccountManagement {
"student", "student",
"teacher" "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; ResultSet rs = null;
String sql = null; String sql = null;
String userType = null; String userType = null;
for(String userTable:userTables){ for(String userTable:userTables){
sql = "SELECT * FROM "+userTable+"WHERE id="+id; sql = "SELECT * FROM "+userTable+" WHERE id=\'"+id+"\'";
try { try {
rs = DBManagement.query(sql); rs = DBManagement.query(sql);
} catch (Exception e) { } catch (Exception e) {
@ -35,8 +37,9 @@ public interface AccountManagement {
if(rs==null){ if(rs==null){
throw new GExcptAccount("id "+id+"don't exists"); throw new GExcptAccount("id "+id+"don't exists");
} }
rs.next();
try { try {
if(rs.getString("password")!=password) if(!rs.getString(2).equals(password))
throw new GExcptAccount("password wrong"); throw new GExcptAccount("password wrong");
return createUser(userType,rs); return createUser(userType,rs);
} catch (SQLException e) { } catch (SQLException e) {
@ -56,7 +59,7 @@ public interface AccountManagement {
} }
static User getUser(String userType){ static User getUser(String userType){
try { try {
return (User)Class.forName("core.user."+userType).getDeclaredConstructor().newInstance(); return (Administrator)Class.forName("core.user."+ Utils.toUpperFristChar(userType)).getDeclaredConstructor().newInstance();
} catch (InstantiationException e) { } catch (InstantiationException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {

@ -50,6 +50,7 @@ public class DBManagement {
con = DBManagement.getConnection(); con = DBManagement.getConnection();
Statement stmt = con.createStatement(); Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql); rs = stmt.executeQuery(sql);
System.out.println(rs);
} catch (SQLException e) { } catch (SQLException e) {
throw new GExcptSQL("Connect Failure"); throw new GExcptSQL("Connect Failure");
}finally { }finally {

@ -1,6 +1,7 @@
package servlet.account; package servlet.account;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
@ -10,6 +11,9 @@ import javax.servlet.http.HttpServletResponse;
import core.user.Student; import core.user.Student;
import core.user.User; import core.user.User;
import core.user.utils.AccountManagement;
import error.GExcptAccount;
import error.GExcptSQL;
@WebServlet("/login") @WebServlet("/login")
public class login extends HttpServlet { public class login extends HttpServlet {
@ -17,12 +21,16 @@ public class login extends HttpServlet {
throws ServletException, IOException { throws ServletException, IOException {
String id=request.getParameter("id"); String id=request.getParameter("id");
String password=request.getParameter("password"); String password=request.getParameter("password");
if(id=="201630126050"&&password=="823169880") User user = null;
{ try {
request.getRequestDispatcher("/service").forward(request,response); user = AccountManagement.login(id,password);
} } catch (GExcptSQL | SQLException gExcptSQL) {
else gExcptSQL.printStackTrace();
response.sendRedirect(""); } catch (GExcptAccount gExcptAccount) {
gExcptAccount.printStackTrace();
}
request.getSession().setAttribute("User",user);
request.getRequestDispatcher("/home.jsp").forward(request,response);
} }
protected void doGet(HttpServletRequest request, HttpServletResponse response) protected void doGet(HttpServletRequest request, HttpServletResponse response)

@ -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);
}
}

@ -24,4 +24,8 @@
<servlet-name>default</servlet-name> <servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern> <url-pattern>*.js</url-pattern>
</servlet-mapping> </servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
</web-app> </web-app>

@ -25,7 +25,7 @@
</div> </div>
<div class="login-cont"> <div class="login-cont">
<div class="logo2">GDMS</div> <div class="logo2">GDMS</div>
<form method="post" action="/login"> <form method="post" action="login">
<input class="input-text username-text" type="text" name="id" <input class="input-text username-text" type="text" name="id"
placeholder="请输入学号"> placeholder="请输入学号">
<input class="input-text password-text" type="password" name="password" <input class="input-text password-text" type="password" name="password"

Loading…
Cancel
Save