?
Solved

LDAP example

Posted on 2001-09-03
3
Medium Priority
?
669 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
chauhanvinit earned 1200 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 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