| Examples

## Directory Example

> This example shows you how to write a program that retrieves attributes from a directory object. It uses an LDAP service provider to access an LDAP service.

> #### Importing the JNDI Directory Classes

> Using your favorite text editor, create a file named `Getattr.java`. You can import either the entire package or only individual classes and interfaces. The following code imports each class that is used from the `javax.naming` and `javax.naming.directory` packages.

> >     import javax.naming.Context;
> >     import javax.naming.directory.InitialDirContext;
> >     import javax.naming.directory.DirContext;
> >     import javax.naming.directory.Attributes;
> >     import javax.naming.NamingException;

> #### Creating an Initial Directory Context

> In the `main()` method of the program, create an initial directory context. This is similar to creating an initial context in the previous naming example, except that you use the constructor for `InitialDirContext`.

> >     Hashtable env = new Hashtable();
> >     env.put(Context.INITIAL_CONTEXT_FACTORY,
> >         "com.sun.jndi.ldap.LdapCtxFactory");
> >     env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

> >     DirContext ctx = new InitialDirContext(env);

> Similar to the naming example, you indicate that you're using the LDAP service provider by setting the `Hashtable` parameter to the `InitialDirContext` constructor appropriately. Details on how to set up the parameters for this constructor are given in The Basics trail. For now, the only thing to understand is that the program by default identifies an LDAP server on the local machine. If your LDAP server is located on another machine or is using another port, then you need to edit the LDAP URL (`"ldap://localhost:389/o=JNDITutorial"`) accordingly. Instructions for setting up a sample LDAP server for this tutorial are given in the Preparations lesson.

> #### Getting a Directory Object's Attributes

> Next, use `getAttributes()` to get an object's attributes. The following code retrieves all of the attributes associated with the object bound to the name `"cn=Ted Geisel, ou=People"`:

> >     Attributes attrs = ctx.getAttributes("cn = Ted Geisel, ou=People");

> #### Extracting the Desired Attribute

> From a set of attributes, `Attributes`, you can ask for a particular attribute by using `Attributes.get()` and then from that attribute get its value. The following line first gets the surname attribute `"sn"` and then invokes `Attribute.get()` on it to get its value:

> >     attrs.get("sn").get();

> #### Catching NamingException

> The method calls shown so far can throw a `NamingException`. For this reason, you need to wrap these calls inside a `try/catch` clause. Here's the code fragment repeated with the `try/catch` clause.

> >     try {

> >         // Create the initial directory context
> >         DirContext ctx = new InitialDirContext(env);

> >         // Ask for all attributes of the object
> >         Attributes attrs = ctx.getAttributes("cn=Ted Geisel, ou=People");

> >         // Find the surname attribute ("sn") and print it
> >         System.out.println("sn: " + attrs.get("sn").get());

> >     } catch (NamingException e) {
> >         System.err.println("Problem getting attribute:" + e);
> >     }

> #### Compiling the Program

> Next, compile the source file using the Java compiler. As with the naming example, to do this you need access to the JNDI classes.

> If the compilation succeeds, then the compiler creates a file named `Getattr.class` in the same directory (folder) as the Java source file (`Getattr.java`). If the compilation fails, then make sure that you typed in and named the program exactly as shown here, using the capitalization shown. If you are still having problems, then see the Common Problems lesson for help.

> #### Running the Program

> As with the naming example, you need access to both the JNDI classes and your example class, `Getattr.class`. You also need access to the LDAP service provider classes (`ldap.jar` and `providerutil.jar`). If you are using the Java 2 SDK, v1.3, then these classes are already included.

> Here's an example of a command line for running `Getattr` and the output it generates.

> >     # java Getattr
> >     sn: Geisel

> Recall that the program was configured with the following property.

> >     env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

> With this configuration, this command queries the LDAP server on machine `localhost` that is listening on port 389, serving the `"o=JNDITutorial"` namespace. (See the Preparations lesson for details on this configuration step.) It asks for the attributes of the entry `"cn=Ted Geisel, ou=People"`. Once it has the attributes, it extracts the surname attribute (`"sn"`). If you have any trouble running this example, then see the Common Problems lesson.

> Examples: End of Lesson

> What's next? Now you can:

> - Continue on in this trail to read about common problems that you might encounter when running or compiling JNDI programs.
> - Go to the The Basics trail for more in-depth lessons on performing naming and directory operations using the JNDI.

| Examples