首页 > 其他分享 >关键代码

关键代码

时间:2022-12-12 11:33:42浏览次数:34  
标签:info 登录 sno 代码 req session 关键 password

@WebServlet("/userLogin")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("准备登录。。。");
        //获取用户的登录角色:查询对应的表格
        String role = req.getParameter("role");
        String user = req.getParameter("user");
        String password = req.getParameter("password");
         // System.out.println("role = " + role);
        HttpSession session = req.getSession();
        Object info = null;
        if (role.equals("管理员")){
            //查询user表 判断登录成功
            User u = new User(user,password);
            UserService userService = new UserServiceImpl();
            info = userService.login(u);
            //info可以放到session中
            if (info!=null){  //登录成功
                System.out.println("管理员登录成功");
                //未登录,不能直接访问管理界面。登录成功后,可以直接访问管理界面pages/studentList2.jsp 但是只能停留5分钟(不更新页面时)
                session.setAttribute("info",info);
                session.setMaxInactiveInterval(60*5);
                req.getRequestDispatcher("allInfoPage").forward(req,resp);//跳转到管理员界面
            }else {
                System.out.println("管理员登录失败");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }

        }else {
            //查询student
            Student s = new Student(user,password);
            StudentService studentService = new StudentServiceImpl();
            info = studentService.login(s);
            if (info!=null){
                System.out.println("学生登录成功");
                session.setAttribute("info",info);
                session.setMaxInactiveInterval(60*5);
                req.getRequestDispatcher("studentInfo").forward(req,resp); //跳转到学生操作界面
            }else {
                System.out.println("学生登录失败");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }


增删改查

在servlet的doget方法中调用service再由service调用的dao的实现类中的 QueryRunner 类型的ComboPooledDataSource对象,通过cp30连接池连接到数据库实现的操作

关键代码

studentDaoimpl
 @Override
    public int addStu(Student s) throws SQLException {
        return qr.update("insert into student(sname,password,sex,score,phone,birthday,cid) values(?,?,?,?,?,?,?)",
                s.getSname(),s.getPassword(),s.getSex(),s.getScore(),s.getPhone(),s.getBirthday(),s.getCid());
    }

    @Override
    public int delStu(String sno) throws SQLException {
        return qr.update("delete from student where sno = ?",sno);
    }

    @Override
    public Student getStuBySno(String sno) throws SQLException {
        return qr.query( "select * from student where sno = ?",new BeanHandler<Student>(Student.class),sno);
    }

    @Override
    public int updateSumbit(Student s) throws SQLException {
        return qr.update("update student set sname = ?,password=?,sex = ?,score = ?,phone = ?,birthday=?, " +
                " status = ?,cid = ? where sno=?",s.getSname(),s.getPassword(),s.getSex(),s.getScore(),s.getPhone(),
                s.getBirthday(),s.getStatus(),s.getCid(),s.getSno());
    };

	```

标签:info,登录,sno,代码,req,session,关键,password
From: https://www.cnblogs.com/leijiajava/p/16975598.html

相关文章