tsx81428 发表于 2024-7-28 15:17:33

重生之深度学习web前端(注册事件绑定数据库)第九天

1、创建注册页面
2、在接口中定义注册的方法
3、在接口的实现类中,实现注册的方法
4、在注册的接口中,处置惩罚(设置编码格式获取账号和暗码
日期由于格式的标题需要处置惩罚,举行日期格式化按照yyyy-MM-ddhh:mm:ss处置惩罚
判定是否注册乐成(注册乐成跳转到登录页面,举行登录
注册失败跳转到注册页面)
核心代码
<!-- 当我们点击登录的时间 ,那么当前表单的所有数据会提交到login这个后端接口行止置惩罚-->
    <!-- get 与post的区别 明文密文     get哀求当点击登录的时间我们当前的form表单数据(账号暗码以及信息)全部会呈如今url地址栏中,明文-->
    <!-- post哀求  是密文当我点击登录哀求login端口时在url上面是所哀求的form表单数据的 -->
    <!-- 长度不同  get哀求的话 url地址栏上面最多出现255个字符-->
    <!-- 长度不同  post哀求的话 url地址栏上面几乎无穷制个字符-->
    <!-- 表单提交数据一律post哀求 -->
    <form action="login" method="post">
    账号:<input type="text" name="userName"/><br/>
    暗码:<input type="password" name="pwd"/><br/>
    <input type="submit" value="登录"/>
    </form>
    <a href="register.jsp">注册</a>

<form action="register" method="post">
    账号:<input type="text" name="userName"/><br/>
    暗码:<input type="password" name="pwd"/><br/>
    <input type="submit" value="注册"/>
    
    </form>

//1.设置哀求和响应的编码格式
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //2.获取注册页面通报过来的数据
        String username=request.getParameter("userName");
        String pwd=request.getParameter("pwd");
        //3.调用注册方法
        UserInfoDao uid=new UserInfoDaoImpl();
        int result=uid.register(new UserInfo(username, pwd,formate(new Date())));
        //4.判定注册乐成还是失败
        if(result>0){
//            注册乐成跳转到登录页面
            response.sendRedirect("login.jsp");
        }else{
//            注册失败跳转到当前注册页面
            response.sendRedirect("register.jsp");
        }
没转换类型之前的结果为外国时间
https://img-blog.csdnimg.cn/direct/7bb62c2555164825ae4bb05f7175c796.png
解决方法
/**
     * 日期格式转换
     * @param date 日期
     * @return 字符串日期年月日小时分钟秒的格式
     */
    public static String formate(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sdf.format(date);
    }
    调用register接口与数据库举行连接
    @Override
    public int register(UserInfo ui) {
        // TODO Auto-generated method stub
        int result=0;
        //1.获取数据库的连接
        Connection conn=DBHelper.getConn();
//        2.书写sql语句
        String sql="inster+ "ert into user_info values(null,?,?,?)";
                
//        3.预编译SQL语句
        try {
            PreparedStatement ps=conn.prepareStatement(sql);
//            4.给参数赋值(占位符)
            ps.setString(1,ui.getName());
            ps.setString(2,ui.getPwd());
            ps.setString(3, ui.getReg_date());
//            5。执行命令、
            result=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
//2.获取页面表单中提交的数据,把页面通报过来的用户名和暗码获取到了
        String name=request.getParameter("userName");
        String pwd=request.getParameter("pwd");
        //3.调用登录的方法
        UserInfoDao uid=new UserInfoDaoImpl();
        UserInfo ui=uid.login(name, pwd);
        if(ui !=null){//意味着登录乐成
            //登陆乐成后进入首页
            response.sendRedirect("index.html");
        }else{
            response.sendRedirect("login.jsp");
        }
感谢各位大佬的观看

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 重生之深度学习web前端(注册事件绑定数据库)第九天