Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class Main {
    public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";

    public static String MY_HOST = "ldap://localhost:389";

    public static String MGR_DN = "uid=yourid, ou=People, o=java.com";

    public static String MGR_PW = "password";

    public static String MY_SEARCHBASE = "o=java.com";

    public static void main(String args[]) throws Exception {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);

        env.put(Context.PROVIDER_URL, MY_HOST);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
        env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

        DirContext ctx = new InitialDirContext(env);

        Person p = new Person();
        ctx.bind("uid=mewilcox, ou=People, o=airius.com", p);
    }
}

class Person {
    String name = "name";
}