登录:

前端页面:

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

 

posted on 2018-09-06 15:05  落雨无晴  阅读(1293)  评论(0编辑  收藏  举报