Tham khảo tài liệu 'building java enterprise applications volume i: architecture phần 5', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Building Java Enterprise Applications Volume I Architecture Create the objectclass to add Attribute objClasses new BasicAttribute objectClass top person organizationalPerson inetOrgPerson Assign the username first name and last name String cnValue new StringBuffer firstName .append .append lastName .toString Attribute cn new BasicAttribute cn cnValue Attribute givenName new BasicAttribute givenName firstName Attribute sn new BasicAttribute sn lastName Attribute uid new BasicAttribute uid username Add password Attribute userPassword new BasicAttribute userpassword password Add these to the container objClasses cn sn givenName uid userPassword Create the entry getUserDN username container Deleting users or any type of subcontext is a much simpler task. All you need to do is identify the name that the subcontext is bound to in this case the user s DN and invoke the destroySubcontext method on the manager s DirContext object. Additionally while the method still throws a NamingException it should trap one specific problem the NameNotFoundException. This exception is thrown when the requested subcontext does not exist within the directory however because ensuring that the DN for the user specified doesn t exist is the point of the deleteUser method this problem is ignored. Whether the specified user is deleted or did not exist prior to the method call is irrelevant to the client. Add the deleteUser method shown here to your source code public void deleteUser String username throws NamingException try getUserDN username catch NameNotFoundException e If the user is not found ignore the error Any other exceptions that might result such as connection failures are still reported through the NamingException that can be thrown in the method. With these two methods in place all user .