Solved

LDAP example

Posted on 2001-09-03
3
661 Views
Last Modified: 2013-11-24
Hi

I am looking for a quick ldap example on how to store something in a directory tree in LDAP.

Lets say I have the following

User = directory
     - name  = value
     - password = value

Can anyone give me quick example on how to do this in LDAP using JNDI?

Thanks
Marius
0
Comment
Question by:Veroland
3 Comments
 
LVL 1

Accepted Solution

by:
chauhanvinit earned 300 total points
ID: 6452401
Hi,
Try this code ,
Make sure u have ldap provider in yur classpath.
You have to download it from sun site.

Initial context would depend upon yur Ldap server.
You will have to provide these values while building an initial context like Server adds, Base DN, authentication (if the server does).




import javax.naming.*;
import javax.naming.directory.*;

import java.util.Hashtable;


class Modattrs {
  public static void main(String[] args) {
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY,          "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://severname");

    try {
     // Create the initial context
     DirContext ctx = new InitialDirContext(env);
     String name = "cn=Ted Geisel, ou=People";

     // Save original attributes
     Attributes orig = ctx.getAttributes(name);

     // Specify the changes to make
     ModificationItem[] mods =new ModificationItem[3];

     mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,new BasicAttribute("dir", "\user\home"));

     mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
          new BasicAttribute("cn", "chauhanvinit"));
         
        mods[2] = new ModificationItem(DirContext.ADD_ATTRIBUTE,new BasicAttribute("passwd", "empty"));
         ctx.modifyAttributes(name, mods);
        // Check attributes
     System.out.println("**** new attributes *****");
     GetattrsAll.printAttrs(ctx.getAttributes(name));

     // Revert changes
     ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);

     // Check that the attributes got restored
     System.out.println("**** reverted to original attributes *****");
     GetattrsAll.printAttrs(ctx.getAttributes(name));

     // Close the context when we're done
         ctx.close();
     } catch (Exception e) {
         e.printStackTrace();
     }
    }
}

yu will get detailed explaination at :-
http://java.sun.com/products/jndi/tutorial/basics/directory/modattrs.html


Bye for now.
0
 
LVL 5

Expert Comment

by:vemul
ID: 7766033
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to chauhanvinit

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7847339
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
difference of if loops 23 62
JavaFX TableView not displaying correctly 3 58
American Express @Work site and Java 4 28
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question