WEB簡單的登入註冊功能(分層)

來源:互聯網
上載者:User

標籤:family   row   date()   action   set   jdbcutil   dao層   Servle   attr   

登入:

前端頁面:

<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);    }}

 

WEB簡單的登入註冊功能(分層)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.