java解析 JNDI示例代码,javajndi示例代码,package cn.o
分享于 点击 36487 次 点评:7
java解析 JNDI示例代码,javajndi示例代码,package cn.o
package cn.outofmemory.snippets.enterprise;import java.util.Hashtable;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.Name;import javax.naming.NameParser;import javax.naming.NamingException;public class ParseJNDICompoundName { public static void main(String[] args) { try { /* * This example parses a compound name using a parser from an LDAP service in which * components are arranged from right to left, delimited by the comma character (,). * The LDAP service is running on localhost and on default port (389) */ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost/o=JNDIExample"); Context ctx = new InitialContext(env); System.out.println("Initial Context created successfully"); NameParser parser = ctx.getNameParser(""); Name dn = parser.parse("cn=byron, ou=People, o=JNDIExample"); System.out.println("Compound name : " + dn); System.out.println("Second component : " + dn.remove(1)); dn.add(0, "c=gr"); System.out.println("After adding component 'c=gr' at the end : " + dn); dn.add("cn=ilias"); System.out.println("After adding component 'cn=ilias' at the beginning : " + dn); } catch (NamingException e) { System.out.println("Exception occurred while parsing Compound name : " + e.getMessage()); } }}
输出:
Initial Context created successfullyCompound name : cn=byron, ou=People, o=JNDIExampleSecond component : ou=PeopleAfter adding component 'c=gr' at the end : cn=byron,o=JNDIExample,c=grAfter adding component 'cn=ilias' at the beginning : cn=ilias,cn=byron,o=JNDIExample,c=gr
用户点评