2024/02/18

package DAO;

import bean.School;
import bean.Stu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StuDAO {
    List<Stu> StuList =new ArrayList<>();
    public boolean Login(Stu stu) //登录
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="select * from stu where id=? and password=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,stu.getId());
            pstm.setString(2,stu.getPassword());
            ResultSet rs=pstm.executeQuery();
            if(rs.next())
            {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public Stu mes(Stu stu) //登录
    {
        Stu stu2=new Stu();
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="select * from stu where id=? and password=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,stu.getId());
            pstm.setString(2,stu.getPassword());
            ResultSet rs=pstm.executeQuery();
            if(rs.next())
            {

                stu2.setId(rs.getString(1));
                stu2.setName(rs.getString(2));
                stu2.setZhengzhi(rs.getString(3));
                stu2.setMath(rs.getString(4));
                stu2.setEnglish(rs.getString(5));
                stu2.setZy(rs.getString("zy"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return stu2;
    }
}
package DAO;

import bean.School;
import bean.Teacher;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TeacherDAO {
    public boolean Login(Teacher teacher) //登录
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="select * from teacher where id=? and password=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,teacher.getId());
            pstm.setString(2,teacher.getPassword());
            ResultSet rs=pstm.executeQuery();
            if(rs.next())
            {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
}
package org.example.schooltest;

import DAO.SchoolDAO;
import DAO.StuDAO;
import DAO.TeacherDAO;
import bean.School;
import bean.Stu;
import bean.Teacher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet(value="/login-servlet")
public class LoginServlet extends HttpServlet {
    private String id;
    private String password;
    private String role;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        role=request.getParameter("role");
        id=request.getParameter("id");
        password=request.getParameter("pwd");
        boolean flag=false;
        if(role.equals("teacher"))
        {
            Teacher teacher=new Teacher();
            teacher.setId(id);
            teacher.setPassword(password);
            flag=new TeacherDAO().Login(teacher);
            if(flag)
            {
                HttpSession session= request.getSession();
                session.setAttribute("User",teacher);
                session.setAttribute("role",role);
                response.sendRedirect("main.jsp");
            }
        }
        else if(role.equals("stu"))
        {
            Stu stu=new Stu();
            stu.setId(id);
            stu.setPassword(password);
            flag=new StuDAO().Login(stu);
            if(flag)
            {
                Stu stu1=new StuDAO().mes(stu);
                HttpSession session= request.getSession();
                System.out.println(stu1.getId());
                session.setAttribute("User",stu1);
                System.out.println(stu1.getZy());
                session.setAttribute("role",role);
                response.sendRedirect("main.jsp");
            }
        }
        else if(role.equals("school"))
        {
            School school=new School();
            school.setId(id);
            school.setPassword(password);
            flag=new SchoolDAO().Login(school);
            if(flag)
            {
                HttpSession session= request.getSession();
                session.setAttribute("User",school);
                session.setAttribute("role",role);
                response.sendRedirect("main.jsp");
            }
        }
        if(!flag)
        {
            request.setAttribute("msg","账号或密码错误");
            try {
                request.getRequestDispatcher("index.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        doGet(request,response);
    }
}

 

1
posted @ 2024-02-18 22:41  伐木工熊大  阅读(17)  评论(0编辑  收藏  举报