• JSP ——第九次作业


    // 注册功能
        public boolean register(Users user) {
            Connection con = null;
            PreparedStatement ps = null;
            try {
                con = getConnection();
                String sql = "insert into users(username,password,email) values(?,?,?)";
                ps = con.prepareStatement(sql);
                ps.setString(1, user.getUsername());// 给sql语句的问号赋值
                ps.setString(2, user.getPassword());
                ps.setString(3, "aaa@qq.com");
                int row = ps.executeUpdate();
                if (row > 0) {
                    return true;
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                closeAll(con, ps, null);
            }
            return false;
        }
    <body>
        <form action="doregister.jsp" method="post">
            用户名:<input type="text" name="uname" /><Br> 密码 :<input
                type="password" name="upwd" /><br> <input type="submit"
                value="注册">
        </form>
    </body>
    <body>
        <%
            request.setCharacterEncoding("utf-8");
            String uname = request.getParameter("uname");
            String upwd = request.getParameter("upwd");
            Users users = new Users();
            users.setUsername(uname);
            users.setPassword(upwd);
            UsersDao ud = new UsersDao();
            if (ud.register(users)) {
                request.getRequestDispatcher("index.jsp").forward(request, response);
            } else
                response.sendRedirect("register.jsp");
        %>
    </body>
    
    doregister.jsp
    

      

    <body>
        <form action="dologin.jsp" method="post">
            用户名:<input type="text" name="uname" value="小白" /><Br> 密码 :<input
                type="password" name="upwd" value="88888" /><br> 验证码:<input
                type="text" name="validationCode"
                onKeyDown="if(event.keyCode==13){form1.submit.focus();}" size="6">
            <%
                int intmethod1 = (int) ((((Math.random()) * 5)) + 1);
                int intmethod2 = (int) ((((Math.random()) * 5)) + 1);
                int intmethod3 = (int) ((((Math.random()) * 5)) + 1);
                int intmethod4 = (int) ((((Math.random()) * 5)) + 1);
                String intsum = intmethod1 + "" + intmethod2 + intmethod3 + intmethod4;
            %>
            <input type="hidden" name="validationCode1" value="<%=intsum%>">
            <img style="height:20px;weight:20px" src="images/<%=intmethod1%>.png">
            <img style="height:20px;weight:20px" src="images/<%=intmethod2%>.png">
            <img style="height:20px;weight:20px" src="images/<%=intmethod3%>.png">
            <img style="height:20px;weight:20px" src="images/<%=intmethod4%>.png">
            <br> <input type="submit" value="登录">
        </form>
    </body>
    
    index.jsp
    

      

    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud = new UsersDao();
        if (ud.login(uname, upwd)
                && request.getParameter("validationCode1").equals(request.getParameter("validationCode"))) {
            //登录成功,创建User对象,并放入session
            Users u = new Users();
            u.setUsername(uname);
            u.setPassword(upwd);
            session.setAttribute("user", u);
            request.getRequestDispatcher("main.jsp").forward(request, response);
        } else
            response.sendRedirect("index.jsp");
    %>
    
    dologin.jsp
    

      

  • 相关阅读:
    在响应式项目中连接设计与开发
    社交APP的痛点及九大流派解析,微信陌陌,咱还约吗
    网传奶茶妹将进入红杉资本
    OLE、OCX和ActiveX控件之间的比较
    Unity3D网络游戏实战(第2版)
    离婚?在Facebook上把已婚状态改为单身就可以!
    同样酷炫但却失败了的谷歌眼镜,能否给Apple Watch一些前车之鉴?
    Apple Watch首批评测放出:有吐槽、有体贴……毒哭了,暖哭了
    如何培养战略领导力,赢得“长久游戏”?
    博客社交已死,数据社交“永生”
  • 原文地址:https://www.cnblogs.com/TSHEN/p/12927626.html
Copyright © 2020-2023  润新知