Day_04_登录实例

1、编写表单前端页面

    
用户名:
密码:

2、Servlet的实现(UserLogin),用来接受数据和判断:

public class UserLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public UserLogin() {
        super();
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        try {
            //接受表单提交的数据
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            //封装到实体对象中,通过这个对象传递数据   
            User user = new User();
            user.setUNAME(username);
            user.setUPASS(password);
            //调用业务层处理数据
            UserService userservice = new UserService();
            //将返回的数据保存,用户存在即返回这个用户的User对象,否则返回null
            User existuser = userservice.login(user);
            //判断是否存在这个用户
            if(existuser!=null) {
                resp.getWriter().write("Welcom! "+ user.getUNAME());
            }else {
                resp.getWriter().write("Login Failed...");
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

3、service的实现(UserService):

public class UserService {

    public User login(User user) throws SQLException {
        UserDao userdao = new UserDao();
        return userdao.login(user);
    }
    
}

4、DAO的实现(UserDao):

public class UserDao {

    
    public User login(User user) throws SQLException {
        QueryRunner query = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select * from userinfo where uname=? and upass=?";
        User existuser = query.query(sql, new BeanHandler(User.class),user.getUNAME(),user.getUPASS());
        return existuser;
    }
    
}

你可能感兴趣的:(Day_04_登录实例)