zoukankan      html  css  js  c++  java
  • WEB简单的登录注册功能(分层)

    登录:

    前端页面:

    <body>
        <form action="/webtext/LogingServlet" method="post">
            <input type="text" name="username"><br>
            <input type="text" name="userpassword"><br>
            <input type="submit" value="登录"><br>
        </form>
    </body>

    domain:

    public class user {
        private int uid;
        private String username;
        private String userpassword;
        public int getUid() {
            return uid;
        }
        public void setUid(int uid) {
            this.uid = uid;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getUserpassword() {
            return userpassword;
        }
        public void setUserpassword(String userpassword) {
            this.userpassword = userpassword;
        }
        @Override
        public String toString() {
            return "user [uid=" + uid + ", username=" + username + ", userpassword=" + userpassword + "]";
        }
    }

    dao层:

    public int load(user user) throws SQLException{
        Connection conn=JDBCUtils.getConn();
        String sql="select * from user where username=? and userpassword=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1, user.getUsername());
        pst.setString(2, user.getUserpassword());
        ResultSet rs = pst.executeQuery();
        int count=0;
        while(rs.next()){
            count=rs.getInt(1);
        }
        JDBCUtils.close(rs, pst, conn);
        return count;
    }

    service层:

    public boolean load(user user){
        boolean flag=false;
        try {
            int count=Dao.load(user);
            if(count==1){
                flag=true;
            }else{
                flag=false;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    登陆的同时检测登陆人数:

    public class LogingServlet extends HttpServlet {
        private service Service=new service();
        public void init() throws ServletException {
            //获取ServletContext对象
            ServletContext servletContext=this.getServletContext();
            //向ServletContext域存入初始count
            int count=0;
            servletContext.setAttribute("count", count);
            
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username=request.getParameter("username");
            String password=request.getParameter("userpassword");
            user user=new user();
            user.setUsername(username);
            user.setUserpassword(password);
            boolean flag=Service.load(user);
            if(flag==true){
                //获取ServletContext对象
                ServletContext servletContext=this.getServletContext();
                //获取当前count值
                int count=(int)servletContext.getAttribute("count");
                //改变count值
                count++;
                response.getWriter().write("success"+user+"ni shi di"+count+"ge fang wen de ren");
                //刷新count值
                servletContext.setAttribute("count", count);
            }else{
                response.getWriter().write("failure");
            }
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }

    注册:

    dao层

    //注册验证
        public int zhuceyz(String username) throws SQLException{
            Connection conn=JDBCUtils.getConn();
            String sql="select username from user where username=?";
            PreparedStatement pst=conn.prepareStatement(sql);
            pst.setString(1, username);
            ResultSet rs = pst.executeQuery();
            int count=0;
            while(rs.next()){
                count=rs.getInt(1);
            }
            JDBCUtils.close(rs, pst, conn);
            return count;
        }
        //存入注册信息
        public int zhuce(user user) throws SQLException{
            Connection conn=JDBCUtils.getConn();
            String sql="insert into user(username,userpassword) values(?,?)";
            PreparedStatement pst=conn.prepareStatement(sql);
            pst.setString(1, user.getUsername());
            pst.setString(2, user.getUserpassword());
            int row=pst.executeUpdate();
            JDBCUtils.close(pst, conn);
            return row;
        }
    }

    service层

    //注册验证
        public boolean zhuceyz(String username){
            boolean flag=false;
            try {
                int count=Dao.zhuceyz(username);
                if(count==0){
                    flag=true;
                }else{
                    flag=false;
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return flag;
        }
        //存入注册信息
        public boolean zhuce(user user){
            boolean flag=false;
            try {
                int count=Dao.zhuce(user);
                if(count==1){
                    flag=true;
                }else{
                    flag=false;
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return flag;
        }
    }

    服务器端

    public class ZhuceServlet extends HttpServlet {
        private service Service=new service();
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            while(true){
                String username=request.getParameter("username");
                String password=request.getParameter("userpassword");
                boolean flag=Service.zhuceyz(username);
                if(flag==false){
                    response.getWriter().write("yibeizhanyong");
                    return;
                }else if(flag==true){
                    user user=new user();
                    user.setUsername(username);
                    user.setUserpassword(password);
                    boolean flag1=Service.zhuce(user);
                    if(flag==true){
                        response.getWriter().write("success"+user);
                    }else{
                        response.getWriter().write("failure");
                    }
                }
                return;
            }
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
  • 相关阅读:
    P6329 【模板】点分树 | 震波
    Luogu P3350 [ZJOI2016]旅行者
    Luogu [ZJOI2015]幻想乡战略游戏
    斐波那契数列简单性质
    Luogu P2056 [ZJOI2007]捉迷藏
    Luogu P4127 [AHOI2009]同类分布
    A funny story in regard to a linux newbie
    Inside the c++ object module 阅读摘要
    java并发编程
    JVM执行引擎总结(读《深入理解JVM》) 早期编译优化 DCE for java
  • 原文地址:https://www.cnblogs.com/Ace-suiyuan008/p/9598413.html
Copyright © 2011-2022 走看看