登陆注册前后端联通

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);
List<String> ls = new ArrayList<>();
ls.add(field);
ResultSet rs = DBManagement.select(ls,table,limits,0,1);
rs.next();
String filePath = rs.getString(0);
List<String[]> resultLs = DBManagement.select(ls,table,limits,0,1);
String filePath = resultLs.get(0)[0];
File file = new File(filePath);
this.getOptions().put("file",file);
return this.getOptions();

@ -17,17 +17,7 @@ public class Search extends Operation {
Map<String, String> limits = (Map<String, String>)this.getOptions().get("limits");
int start = (int)this.getOptions().get("start");
int end = (int)this.getOptions().get("end");
ResultSet rs = 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);
}
List<String[]> ls = DBManagement.select(fields,table,limits,start,end);
this.getOptions().put("result",ls);
return this.getOptions();
}

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

@ -30,7 +30,7 @@ public class ProcessManagement {
//Map<graduation_design_id.x_status,status>
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;
for(Map.Entry<String,String> entry:userTables.entrySet()){
String[] ti = entry.getKey().split(".");

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

@ -1,6 +1,7 @@
package dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -29,6 +30,32 @@ public class DBManagement {
"graduation_design_reply",
"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>
public static Map<String,String> graduationDesignStudentTables = new HashMap<>(){
{
@ -79,7 +106,7 @@ public class DBManagement {
}
return null;
}
public static Map<String,String> getUserTables(String userType){
public static Map<String,String> getUserStatusTables(String userType){
if(userType.equals("student")){
return graduationDesignStudentTables;
}
@ -88,7 +115,18 @@ public class DBManagement {
}
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 {
Delete delete = new Delete(table, limits);
String sql = delete.getSQL();
@ -99,7 +137,7 @@ public class DBManagement {
String sql = insert.getSQL();
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);
String sql = select.getSQL();
return query(sql);
@ -109,15 +147,24 @@ public class DBManagement {
String sql = update.getSQL();
update(sql);
}
public static ResultSet query(String sql) throws Exception {
public static List<String[]> query(String sql) throws Exception {
if(!ifInit) return null;
ResultSet rs = null;
List<String[]> ls;
Connection con = null;
try{
con = DBManagement.getConnection();
Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql);
System.out.println(rs);
ResultSet rs = stmt.executeQuery(sql);
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) {
throw new GExcptSQL("Connect Failure");
}finally {
@ -129,7 +176,7 @@ public class DBManagement {
}
}
}
return rs;
return ls;
}
public static void update(String sql) throws GExcptSQL {
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 static void main(String[] args){
String student_id = "001";
String teacher_id = "002";
core.process.Process p = new C_StudentSelectTeacher();
try {
((C_StudentSelectTeacher) p).select(student_id,teacher_id);
} catch (Exception e) {
e.printStackTrace();
}
int i=0;
do{
i++;
System.out.println(i);
}while(i<3);
List<String> list = new ArrayList<>();
list.add("q");
list.add("w");

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

@ -17,21 +17,28 @@ import error.GExcptSQL;
@WebServlet("/login")
public class login extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id=request.getParameter("id");
String password=request.getParameter("password");
User user = null;
try {
user = AccountManagement.login(id,password);
} catch (GExcptSQL | SQLException gExcptSQL) {
gExcptSQL.printStackTrace();
} catch (GExcptAccount gExcptAccount) {
gExcptAccount.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
request.getSession().setAttribute("user",user);
request.getRequestDispatcher("/home.jsp").forward(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id=request.getParameter("id");
String password=request.getParameter("password");
User user = null;
try {
user = AccountManagement.login(id,password);
} catch (GExcptSQL | SQLException gExcptSQL) {
gExcptSQL.printStackTrace();
response.sendRedirect("/op_fail.jsp");
return;
} catch (GExcptAccount gExcptAccount) {
gExcptAccount.printStackTrace();
response.sendRedirect("/op_fail.jsp");
return;
} 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 class="register-cont">
<form method="post" action="login">
<form method="post" action="register">
<input class="register-input-text sno-text" type="text" name="id"
placeholder="学号或职工号">
<input class="register-input-text password-text" type="password" name="password"
@ -53,7 +53,7 @@
placeholder="请输入电话">
<input class="register-input-text check-password-text" type="password" name="password-check"
placeholder="请确认密码">
<select class="id-check" name="usertype">
<select class="id-check" name="userType">
<option value="">身份</option>
<option value="student">学生</option>
<option value="teacher">老师</option>

Loading…
Cancel
Save