1,登录前端和后端对接

wlf
zhai_lw 6 years ago
parent 90d6124e61
commit 04a929c714

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

@ -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) {

@ -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 {

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

@ -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>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
</web-app>

@ -25,7 +25,7 @@
</div>
<div class="login-cont">
<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"
placeholder="请输入学号">
<input class="input-text password-text" type="password" name="password"

Loading…
Cancel
Save