登陆注册前后端联通

zgl
zhai_lw 6 years ago
parent fe2b89a5fd
commit b1c174822a

@ -17,9 +17,8 @@ public class DownloadFile extends Operation{
String field = core.operation.utils.Utils.getFileField(table); String field = core.operation.utils.Utils.getFileField(table);
List<String> ls = new ArrayList<>(); List<String> ls = new ArrayList<>();
ls.add(field); ls.add(field);
ResultSet rs = DBManagement.select(ls,table,limits,0,1); List<String[]> resultLs = DBManagement.select(ls,table,limits,0,1);
rs.next(); String filePath = resultLs.get(0)[0];
String filePath = rs.getString(0);
File file = new File(filePath); File file = new File(filePath);
this.getOptions().put("file",file); this.getOptions().put("file",file);
return this.getOptions(); return this.getOptions();

@ -17,17 +17,7 @@ public class Search extends Operation {
Map<String, String> limits = (Map<String, String>)this.getOptions().get("limits"); Map<String, String> limits = (Map<String, String>)this.getOptions().get("limits");
int start = (int)this.getOptions().get("start"); int start = (int)this.getOptions().get("start");
int end = (int)this.getOptions().get("end"); int end = (int)this.getOptions().get("end");
ResultSet rs = DBManagement.select(fields,table,limits,start,end); List<String[]> ls = DBManagement.select(fields,table,limits,start,end);
ResultSetMetaData rsm = rs.getMetaData() ;
int columnCount = rsm.getColumnCount();
List<String[]> ls = new ArrayList<>();
while(rs.next()){
String[] s = new String[columnCount];
for(int i=0;i<columnCount;i++){
s[i] = rs.getString(i);
}
ls.add(s);
}
this.getOptions().put("result",ls); this.getOptions().put("result",ls);
return this.getOptions(); return this.getOptions();
} }

@ -28,9 +28,8 @@ public class UploadFileOperation extends Operation {
List<String> ls = new ArrayList<>(); List<String> ls = new ArrayList<>();
ls.add(field); ls.add(field);
ResultSet rs = DBManagement.select(ls,table,limits,0,1); List<String[]> resultLs = DBManagement.select(ls,table,limits,0,1);
rs.next(); String lastFilePath = resultLs.get(0)[0];
String lastFilePath = rs.getString(0);
new File(lastFilePath).delete(); new File(lastFilePath).delete();
DBManagement.update(table,vMap,limits); DBManagement.update(table,vMap,limits);
return this.getOptions(); return this.getOptions();

@ -30,7 +30,7 @@ public class ProcessManagement {
//Map<graduation_design_id.x_status,status> //Map<graduation_design_id.x_status,status>
public static Map<String,Character> getStatus(User user){ public static Map<String,Character> getStatus(User user){
Map<String,String> userTables = DBManagement.getUserTables(user.getType()); Map<String,String> userTables = DBManagement.getUserStatusTables(user.getType());
String graduationDesignId = null; String graduationDesignId = null;
for(Map.Entry<String,String> entry:userTables.entrySet()){ for(Map.Entry<String,String> entry:userTables.entrySet()){
String[] ti = entry.getKey().split("."); String[] ti = entry.getKey().split(".");

@ -20,7 +20,7 @@ import static dao.DBManagement.userTables;
public interface AccountManagement { public interface AccountManagement {
static User login(String id, String password) throws Exception { static User login(String id, String password) throws Exception {
ResultSet rs = null; List<String[]> ls = null;
String userType = null; String userType = null;
for(String userTable:userTables){ for(String userTable:userTables){
List<String> columns = new ArrayList<>(); List<String> columns = new ArrayList<>();
@ -28,26 +28,22 @@ public interface AccountManagement {
Map<String,String> limits = new HashMap<>(); Map<String,String> limits = new HashMap<>();
limits.put("id",id); limits.put("id",id);
try { try {
rs = DBManagement.select(columns,userTable,limits,0,1); ls = DBManagement.select(columns,userTable,limits,0,1);
} catch (Exception e) { } catch (Exception e) {
throw new GExcptSQL("QUERY\n\t"+id+"\nfailure"); throw new GExcptSQL("QUERY\n\t"+id+"\nfailure");
} }
if(rs!=null){ if(ls.size()!=0){
userType = userTable; userType = userTable;
break; break;
} }
} }
if(rs==null){ String[] userInfo = ls.get(0);
throw new GExcptAccount("id "+id+"don't exists"); if(!userInfo[1].equals(password))
}
rs.next();
if(!rs.getString(2).equals(password))
throw new GExcptAccount("password wrong"); throw new GExcptAccount("password wrong");
Map<String, String> vMap = new HashMap<>(); Map<String, String> vMap = new HashMap<>();
ResultSetMetaData rsm = rs.getMetaData(); String[] info = DBManagement.getUserInfoTables(userType);
rs.next(); for(int i=0;i<userInfo.length;i++){
for(int i=0;i<rsm.getColumnCount();i++){ vMap.put(info[i],userInfo[i]);
vMap.put(rsm.getCatalogName(i),rs.getString(i));
} }
return createUser(userType,vMap); return createUser(userType,vMap);
} }

@ -1,6 +1,7 @@
package dao; package dao;
import java.sql.*; import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -29,6 +30,32 @@ public class DBManagement {
"graduation_design_reply", "graduation_design_reply",
"graduation_design_reply_opinion_record_score" "graduation_design_reply_opinion_record_score"
}; };
public static String[] studentInfo = {
"id",
"password",
"name",
"e_mail_location",
"phone_number",
"grade",
"profession_code"
};
public static String[] teacherInfo = {
"id",
"password",
"name",
"e_mail_location",
"phone_number",
"job_title",
"education",
"profession_code"
};
public static String[] administratorInfo = {
"id",
"password",
"name",
"e_mail_location",
"phone_number"
};
//<table1.x_id,table2.x_status> //<table1.x_id,table2.x_status>
public static Map<String,String> graduationDesignStudentTables = new HashMap<>(){ public static Map<String,String> graduationDesignStudentTables = new HashMap<>(){
{ {
@ -79,7 +106,7 @@ public class DBManagement {
} }
return null; return null;
} }
public static Map<String,String> getUserTables(String userType){ public static Map<String,String> getUserStatusTables(String userType){
if(userType.equals("student")){ if(userType.equals("student")){
return graduationDesignStudentTables; return graduationDesignStudentTables;
} }
@ -88,7 +115,18 @@ public class DBManagement {
} }
return null; return null;
} }
public static String[] getUserInfoTables(String userType){
if(userType.equals("administrator")){
return administratorInfo;
}
if(userType.equals("student")){
return studentInfo;
}
if(userType.equals("teacher")){
return teacherInfo;
}
return null;
}
public static void delete(String table, Map<String,String> limits) throws GExcptSQL { public static void delete(String table, Map<String,String> limits) throws GExcptSQL {
Delete delete = new Delete(table, limits); Delete delete = new Delete(table, limits);
String sql = delete.getSQL(); String sql = delete.getSQL();
@ -99,7 +137,7 @@ public class DBManagement {
String sql = insert.getSQL(); String sql = insert.getSQL();
update(sql); update(sql);
} }
public static ResultSet select(List<String> columns, String table, Map<String,String> limits, int startRow, int endRow) throws Exception { public static List<String[]> select(List<String> columns, String table, Map<String,String> limits, int startRow, int endRow) throws Exception {
Select select = new Select(columns,table,limits,startRow,endRow); Select select = new Select(columns,table,limits,startRow,endRow);
String sql = select.getSQL(); String sql = select.getSQL();
return query(sql); return query(sql);
@ -109,15 +147,24 @@ public class DBManagement {
String sql = update.getSQL(); String sql = update.getSQL();
update(sql); update(sql);
} }
public static ResultSet query(String sql) throws Exception { public static List<String[]> query(String sql) throws Exception {
if(!ifInit) return null; if(!ifInit) return null;
ResultSet rs = null; List<String[]> ls;
Connection con = null; Connection con = null;
try{ try{
con = DBManagement.getConnection(); con = DBManagement.getConnection();
Statement stmt = con.createStatement(); Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql); ResultSet rs = stmt.executeQuery(sql);
System.out.println(rs); ResultSetMetaData rsm = rs.getMetaData() ;
int columnCount = rsm.getColumnCount();
ls = new ArrayList<>();
while(rs.next()){
String[] s = new String[columnCount];
for(int i=0;i<columnCount;i++){
s[i] = rs.getString(i+1);
}
ls.add(s);
}
} catch (SQLException e) { } catch (SQLException e) {
throw new GExcptSQL("Connect Failure"); throw new GExcptSQL("Connect Failure");
}finally { }finally {
@ -129,7 +176,7 @@ public class DBManagement {
} }
} }
} }
return rs; return ls;
} }
public static void update(String sql) throws GExcptSQL { public static void update(String sql) throws GExcptSQL {
if(!ifInit) return; if(!ifInit) return;

@ -0,0 +1,20 @@
package filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "OnlineCheck")
public class OnlineCheck implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}

@ -11,14 +11,12 @@ import java.util.Map;
public class test { public class test {
public static void main(String[] args){ public static void main(String[] args){
String student_id = "001"; int i=0;
String teacher_id = "002"; do{
core.process.Process p = new C_StudentSelectTeacher(); i++;
try { System.out.println(i);
((C_StudentSelectTeacher) p).select(student_id,teacher_id); }while(i<3);
} catch (Exception e) {
e.printStackTrace();
}
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
list.add("q"); list.add("q");
list.add("w"); list.add("w");

@ -5,6 +5,7 @@ import core.user.utils.AccountManagement;
import error.GExcptAccount; import error.GExcptAccount;
import error.GExcptSQL; import error.GExcptSQL;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -13,34 +14,37 @@ import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class S_Register extends HttpServlet { @WebServlet("/register")
public class Register extends HttpServlet {
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
Map<String,String> user_info=new HashMap<>(); Map<String,String> user_info=new HashMap<>();
String id=request.getParameter("id"); String id=request.getParameter("id");
user_info.put("id",id); user_info.put("id",id);
String name=request.getParameter("name"); String name=request.getParameter("name");
user_info.put("name",name); user_info.put("name",name);
String e_mail=request.getParameter("e-mail"); String e_mail=request.getParameter("e_mail_location");
user_info.put("e_mail",e_mail); user_info.put("e_mail_location",e_mail);
String phone_number=request.getParameter("phone-number"); String phone_number=request.getParameter("phone_number");
user_info.put("phone_number",phone_number); user_info.put("phone_number",phone_number);
String password=request.getParameter("password"); String password=request.getParameter("password");
user_info.put("password",password); user_info.put("password",password);
String userType = request.getParameter("userType");
User user=null; User user=null;
try { try {
user = AccountManagement.register("student",user_info); user = AccountManagement.register(userType,user_info);
} catch (GExcptSQL gExcptSQL) { } catch (GExcptSQL gExcptSQL) {
gExcptSQL.printStackTrace(); gExcptSQL.printStackTrace();
response.sendRedirect("/op_fail.jsp");
} catch (GExcptAccount gExcptAccount) { } catch (GExcptAccount gExcptAccount) {
gExcptAccount.printStackTrace(); gExcptAccount.printStackTrace();
response.sendRedirect("/op_fail.jsp");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
response.sendRedirect("/op_fail.jsp");
} }
//注册失败 request.getSession().setAttribute("user",user);
request.getSession().setAttribute("User",user); response.sendRedirect("/home.jsp");
request.getRequestDispatcher("/home.jsp").forward(request,response); return;
} }
} }

@ -17,21 +17,28 @@ import error.GExcptSQL;
@WebServlet("/login") @WebServlet("/login")
public class login extends HttpServlet { public class login extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
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");
User user = null; User user = null;
try { try {
user = AccountManagement.login(id,password); user = AccountManagement.login(id,password);
} catch (GExcptSQL | SQLException gExcptSQL) { } catch (GExcptSQL | SQLException gExcptSQL) {
gExcptSQL.printStackTrace(); gExcptSQL.printStackTrace();
} catch (GExcptAccount gExcptAccount) { response.sendRedirect("/op_fail.jsp");
gExcptAccount.printStackTrace(); return;
} catch (Exception e) { } catch (GExcptAccount gExcptAccount) {
e.printStackTrace(); gExcptAccount.printStackTrace();
} response.sendRedirect("/op_fail.jsp");
request.getSession().setAttribute("user",user); return;
request.getRequestDispatcher("/home.jsp").forward(request,response); } catch (Exception e) {
} e.printStackTrace();
response.sendRedirect("/op_fail.jsp");
return;
}
request.getSession().setAttribute("user",user);
response.sendRedirect("/home.jsp");
return;
}
} }

@ -40,7 +40,7 @@
</div> </div>
<div class="register-cont"> <div class="register-cont">
<form method="post" action="login"> <form method="post" action="register">
<input class="register-input-text sno-text" type="text" name="id" <input class="register-input-text sno-text" type="text" name="id"
placeholder="学号或职工号"> placeholder="学号或职工号">
<input class="register-input-text password-text" type="password" name="password" <input class="register-input-text password-text" type="password" name="password"
@ -53,7 +53,7 @@
placeholder="请输入电话"> placeholder="请输入电话">
<input class="register-input-text check-password-text" type="password" name="password-check" <input class="register-input-text check-password-text" type="password" name="password-check"
placeholder="请确认密码"> placeholder="请确认密码">
<select class="id-check" name="usertype"> <select class="id-check" name="userType">
<option value="">身份</option> <option value="">身份</option>
<option value="student">学生</option> <option value="student">学生</option>
<option value="teacher">老师</option> <option value="teacher">老师</option>

Loading…
Cancel
Save