Educational administration management system realizes 1-account login

Educational administration management system - account login implementation

Project structure

(1) Login interface

Enter the user name and password, select the identity, and log in to the corresponding student interface and teacher interface.

(2) System student interface

Display the student's name and student number on the interface.
Display basic information of students.
Query course information: enter course number and teacher number. Show course information. Course number and teacher number can be blank.
Course selection interface: enter course number and teacher number. Take the appropriate course. To display an error message (not as good as a course number error or a teacher number error).
Drop out interface: displays the class list of the student, and the student can select the corresponding content for drop out submission.
Display student's report form: select the corresponding semester to display the final report form.
Course schedule: displays the semester and the contents of the course schedule.
Exit: exit the current account. Go back to the login screen.

(3) System teacher interface

Display the teacher's name and student number on the interface
Display basic information of Teachers
Check the student grades in the class. Select the semester, jump out of the course schedule, click the course number, and a modal box will pop up to display the students' grades in the class.
Overall distribution of student performance. Select semester, the course drop-down menu will automatically change to the course of the current semester, and select course. According to the course and semester, we can see the overall distribution of students' scores.
Student performance login. Show the course of the current semester, show the student table according to the semester selected by the teacher, and finally submit the scores.
Course information. Select the semester and course and display the course table with the course details. Click on the course number, there is a student table, displaying the details of students.

Different function realization

1. Login interface

Front end: bootstrap+jquery

Submit using the form. Form design:

<!--Enter the user name and password in the input box, and select the identity.-->
<!--required Indicates that it must be entered.-->
<div class="container"><br>
        <form id="login_form" name="login_form" onsubmit="return false" action="##" method="get">
            <div class="form-group">
                <lable for="username">User:</lable>
                <input type="text" class="form-control" id="username" placeholder="enter one user name" name="id" required>
            </div>
            <div class="form-group">
                <lable for="password">Password:</lable>
                <input type="password" class="form-control" id="password" placeholder="Please input a password" name="pwd" required>

            </div>
            <div class="radio">
                <lable><input type="radio" name="identity" value="student" required>Student</lable>
                <lable><input type="radio" name="identity" value="teacher" required>Teacher</lable>
            </div>
            <input id="login" type="button" class="btn btn-primary" value="Land" onclick="login_check()">
            <p id="success"></p>
        </form>
    </div>

Data is delivered via ajax:

function login_check() {//Land
            var check_res;
            var is_send;
            //Data returned in the form
            var id = $("input[name=id]").val();
            var pwd = $("input[name=pwd]").val();
            var identity = $("input[name=identity]:checked").val();
            console.log(id);
            console.log(pwd);
            console.log(identity);
            $.ajax({
                type: "get",
                //Data type of server response
                dataType: "text",
                url: "http://localhost:8080/login",
                //Send json type data
                data: {
                    "id": id,//User name
                    "pwd": pwd,//Password
                    "identity": identity
                },//identity
                async:false,//synchronization
                //Send to server default data type
                //Allow cross domain requests
                xhrFields:{
                  withCredentials:true
                },
                // crossDomain:true,
                //Specifies that string data is returned from the server
                // contentType: 'application/json',
                success: function (result) {
                    console.log("sending");
                    check_res = result;
                    is_send = true;
                    console.log(result);
                    if (check_res == "true_student") {
                        console.log("logging!")
                        $("#Success). Text ("login succeeded");
                        //Jump to student interface
                        $(window).attr('location','./studentPage/studentHomePage.html');
                    } else if(check_res=="true_teacher"){
                        console.log("logging!");
                        $("#Success). Text ("login succeeded");
                        //Jump to teacher interface
                        $(window).attr('location','./teacherPage/teacherHomePage.html');
                    } else if(check_res=="false"){
                        console.log("logging error!");
                        $("#success").text(" wrong user name or password! "";
                    } else{
                        console.log("haven't receive data");
                    }
                },
                error: function (XMLHttpRequest,textStatus) {
                    $("#success").text(" error ");
                    alert(XMLHttpRequest.readyState);//Debug status code
                    alert(textStatus);
                    alert(XMLHttpRequest.status);
                    is_send = false;
                    console.log("error!");
                },
            });
        }

Backend: servelt

1. Students:
public class student {
    String id;
    String name;
    String sex;
    String college;
    String hometown;
    String phone_number;
    String birthday;
    public student(String id,String name,String sex,String college,
                   String hometown,String phone_number,String birthday){
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.college = college;
        this.hometown = hometown;
        this.phone_number = phone_number;
        this.birthday = birthday;
    }
    public void setId(String id){
        this.id = id;
    }
    public String getId(){
        return id;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setSex(String sex){
        this.sex=sex;
    }
    public String getSex(){
        return sex;
    }
    public void setCollege(String college){
        this.college=college;
    }
    public String getCollege(){
        return college;
    }
    public void setHometown(String hometown){
        this.hometown=hometown;
    }
    public String getHometown(){
        return hometown;
    }
    public void setPhone_number(String phone_number){
        this.phone_number=phone_number;
    }
    public String getPhone_number(){
        return phone_number;
    }
    public void setBirthday(String birthday){
        this.birthday = birthday;
    }
    public String getBirthday(){
        return birthday;
    }
}
2. Teachers:
public class teacher {
    String id;//Job number
    String name;//Full name
    String gender;//Gender
    String birthday;//Date of birth
    String level;//level
    double wages;//wages
    String department;//Faculty and department
    public void setId(String id){
        this.id=id;
    }
    public String getId(){
        return id;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setGender(String gender){
        this.gender=gender;
    }
    public String getGender(){
        return gender;
    }
    public void setBirthday(String birthday){
        this.birthday = birthday;
    }
    public String getBirthday(){
        return birthday;
    }
    public void setLevel(String level){
        this.level=level;
    }
    public String getLevel(){
        return level;
    }
    public void setWages(double wages){
        this.wages=wages;
    }
    public double getWages(){
        return wages;
    }
    public void setDepartment(String department){
        this.department=department;
    }
    public String getDepartment(){
        return department;
    }
}

3. Login: servlet class
private static final long serialVersionUID = 1L;
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/school";
    static final String USER = "root";
    static final String PASS = "ammy990427";
    JsonArray json = null;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        Connection conn = null;
        Statement stmt = null;
        System.out.println("Login interface: get");
        //Get form information
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String identity = request.getParameter("identity");
        String userName = request.getParameter("id");
        String password = request.getParameter("pwd");
        System.out.printf("%s %s %s\n",identity,userName,password);
        response.setContentType("text/html;charset=UTF-8");
        try{
            //Database connection
            student s = new student("null","null","null","null","null",
                    "null","null");
            teacher t = new teacher();
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            stmt = conn.createStatement();
            String sql;
            PrintWriter out = response.getWriter();
            if(identity.equals("student")){
                System.out.println("Current login status: Student");
                sql="select stid,passwd from stuid where stid="+userName+";";
            }else{
                System.out.println("Current login identity: Teacher");
                sql="select gh,passwd from teacherid where gh="+userName+";";
            }
            ResultSet rs = stmt.executeQuery(sql);
            //Write the current record to the session
            HttpSession session = request.getSession();
            if(!rs.next()){//If the user is not found in the database
                //Return string
                out.print("false");
                System.out.println("1");
            }else if(!rs.getString(2).equals(password)) {//If the passwords don't match
                //Return string
                out.print("false");
                System.out.println("2");
            }else{//If the user matches
//                response.setStatus(302);
                if(identity.equals("student")){
                    //Return string
                    out.print("true_student");
                    s.setId(userName);
                    session.setAttribute("stu_id",userName);
                    System.out.println((String) session.getAttribute("stu_id"));
                    System.out.println(session.getId());
                    System.out.println("student login");
                }else if(identity.equals("teacher")){
                    //Return string
                    out.print("true_teacher");
                    t.setId(userName);
                    session.setAttribute("tea_id",userName);
                    System.out.println("teacher login");
                }
            }
            out.flush();
            out.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
Code logic:

1. Get the form content, user name, password and identity
2. Connect to the database (jdbc), and verify the password according to the identity and user name:
There are two situations:
(1) User name does not exist
(2) Password error
(3) Verification passed
3. If it is the first two cases, the string false will be returned
4. By returning the string true and recording the student's information in the session.
5. The front end makes different responses according to the returned string information. If false, the user name or password error field will be displayed. If true, the student or teacher interface will be displayed.

tips:

1. session is used to store the login information, so add the following to the ajax in the front end:

xhrFields:{withCredentials:true},

2. There should be a filter in the servlet, which is often intercepted because of the CORS protocol (I don't understand why):

public class Filter implements javax.servlet.Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//        chain.doFilter(req, resp);
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpServletRequest request = (HttpServletRequest) req;
        String origin = request.getHeader("Origin");
        String[] allowDomain = {"http://localhost:63343","http://localhost:63342"};
        Set<String> allowOrigin = new HashSet<String>(Arrays.asList(allowDomain));
        if(allowOrigin.contains(origin)){
            response.setHeader("Access-Control","no-cache");
            response.setHeader("Access-Control-Allow-Origin", origin);
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//            response.setHeader("Access-Control-Max-Age", "3600");
//            response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
        }
        response.setCharacterEncoding("UTF-8");
        System.out.println("filter implement");
        chain.doFilter(request,response);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

In allowDomain is the allowed http header (I don't know what to say). In the URL pattern in the deployment, the scope is all servlet s, which is "/ *".
3. The deployment in web.xml will not be described in detail.


Published 1 original article, praised 0, visited 8
Private letter follow

Tags: Session JDBC SQL JSON

Posted on Sat, 08 Feb 2020 01:08:37 -0800 by joix