重生之深度学习WEB前端补充第八天的

我们现在要实现的场景是登录:
1.创建登录的页面
2.创建用户的实体类
3.创建用户接口,在接口中定义登录的方法
4.创建用户接口的实现类,实现用户接口,实现接口中的登录方法
5.创建servlet,servlet中的地址与页面提交表单的地址,要是一样的。
6.在servlet中处理登录的业务逻辑(根据页面的请求方式,在servlet对应的方法中实现,get请求==》doget,post==>dopost
6.1设置请求和响应的编码格式
6.2获取页面传递过来的数据
6.3调用登录方法,把获取到的参数(账号和密码)传递到方法中
6.4根据用户对象来进行判断是否登录成功(
如果用户对象不为空意味着登录成功
登录成功后,页面跳转进首页
如果用户对象为空意味着登录失败
登录失败后,页面跳转到登录页面,继续登录)
)

核心代码
getId得到id的属性值
    public int getId() {
        return id;
    }
//        setId给id赋值
    
    public void setId(int id) {
        this.id = id;
    }

//  全参
    public UserInfo(int id, String name, String pwd, String reg_date) {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.reg_date = reg_date;
    }
// 无参
    public UserInfo() {
    
    }
//无id的 (因为id是自增的)
    public UserInfo(String name, String pwd, String reg_date) {
        
        this.name = name;
        this.pwd = pwd;
        this.reg_date = reg_date;
    }

//1.获得数据库连接对象
        Connection conn=DBHelper.getConn();
        //2.书写sql语句
        String sql="select * from user_info where username=? and passwords=? ";
        //3.预编译sql语句,得到preperment (sql包下面的)的对象
        try {
            PreparedStatement ps=conn.prepareStatement(sql);
            //4.给参数(占位符)赋值
            ps.setString(1, name);
            ps.setString(2, pwd);
            //5.执行查询命令,会得到结果集
            ResultSet rs=ps.executeQuery();
            //6.循环从结果集中读取数据给实体类对象属性赋值,因为我们登陆查询的结果要么一条,要么没有 
            if(rs.next()){//返回值为true 意味着找到数据了,反之;
                userinfo = new UserInfo(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
                
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        //1.设置请求和响应的编码格式
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        //2.获取页面表单中提交的数据,把页面传递过来的用户名和密码获取到了
        String name=request.getParameter("userName");
        String pwd=request.getParameter("pwd");
        //3.调用登录的方法
        UserInfoDao uid=new UserInfoDaoImpl();
        UserInfo ui=uid.login(name, pwd);
        System.out.println(name);
        System.out.println(pwd);
        System.out.println(ui);
        //4.判断是否登录成功
        if(ui !=null){//意味着登录成功
            //登陆成功后进入首页
            response.sendRedirect("index.html");
        }else{
            response.sendRedirect("login.jsp");
        }
    }

//    定义三个常量(数据库连接字符串,账号,密码)
    public static final String URL="jdbc:mysql://localhost:3306/timi?userUnicode=true&characterEncoding=utf-8";
    public static final String NAME="root";
    public static final String PWD="123456";
    
//    定义数据库连接对象connection
    public static Connection conn; //导包的时候注意,导入 .sql包下面的connection
    
    /**
     * 获取数据库连接的方法
     * @return 数据库链接对象
     */
    
    public static Connection getConn(){
    
        try {
    //        1. 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2.通过驱动管理器里面的getConnection方法来获取数据库的连接对象
            try {
                conn=DriverManager.getConnection(URL, NAME, PWD);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            return conn;
        
    }


<!-- 当我们点击登录的时候 ,那么当前表单的所有数据会提交到login这个后端接口去处理-->
    <!-- get 与post的区别 明文密文     get请求当点击登录的时候我们当前的form表单数据(账号密码以及信息)全部会呈现在url地址栏中,明文-->
    <!-- post请求  是密文当我点击登录请求login端口时在url上面是看不见所请求的form表单数据的 -->
    <!-- 长度不同  get请求的话 url地址栏上面最多出现255个字符-->
    <!-- 长度不同  post请求的话 url地址栏上面最多出现无限制个字符-->
    <!-- 表单提交数据一律post请求 -->



感谢各位大佬的观看
        

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值