Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

LDAP example

Posted on 2001-09-03
3
Medium Priority
?
671 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 October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

636 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