InitialDirContext
InitialDirContext is a concrete implementation of the DirContext interface in the Java Naming and Directory Interface (JNDI). It extends the standard InitialContext and serves as the entry point for directory-oriented operations. The class relies on the JNDI service provider mechanism to locate a directory service implementation that can handle directory-specific tasks such as retrieving attributes, performing searches, and modifying attributes.
InitialDirContext can be created with no arguments or with an environment specified as a hashtable. When an
Once created, InitialDirContext inherits directory-specific operations from DirContext, such as getAttributes, search, and modifyAttributes. These methods
A common pattern is to initialize the context with LDAP settings and then perform directory queries. Example:
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
DirContext ctx = new InitialDirContext(env);
Attributes attrs = ctx.getAttributes("cn=Manager,dc=example,dc=com");
InitialContext and DirContext, as well as provider-specific factories such as com.sun.jndi.ldap.LdapCtxFactory.