1.dao层接口
public interface UserDao {

User devUserLogin(String username, String password) throws Exception;

}

2.daoImpl
public class UserDaoImpl implements UserDao {

QueryRunner qy = new QueryRunner(JDBCUtils.instance());

@Override
public User devUserLogin(String username, String password) throws Exception {

    String sql = "select * from user where username= ? and password= ?";
    //return qr.query(sql,new BeanHandler<Shepi>(Shepi.class),empno);
    return qy.query(sql,new BeanHandler<User>(User.class),username,password);

}

3.service层
/**

  • 登录
    */
    public interface DevUserService {

    /**

    • 登录功能
    • @param username 用户名
    • @param password 密码
    • @return 用户对象
      */
      User devUserLogin(String username,String password) throws Exception;
      }

4.serviceImpl
public class DevUserServiceImpl implements DevUserService {

UserDao ud = new UserDaoImpl();


@Override
public User devUserLogin(String username, String password) throws Exception {

  return ud.devUserLogin(username,password);
    //return null;
}

}

5.controller层
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {

DevUserService devUserService = new DevUserServiceImpl();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //设置编码
    req.setCharacterEncoding("utf-8");

    //获取请求参数
    String username = req.getParameter("username");
    String password = req.getParameter("password");


    //封装user对象
    User loginUser = new User();
    loginUser.setUsername(username);
    loginUser.setPassword(password);

    if(username != null){
        req.setAttribute("username",username);
    }

    try {
        User user = devUserService.devUserLogin(username, password);
        if(user == null){
            req.getRequestDispatcher("/failServlet").forward(req,resp);
        }else{
            req.setAttribute("user",user);

            req.getRequestDispatcher("/emp/list03").forward(req,resp);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }


}


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req, resp);
}

}