⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showsomeattr.java~100~

📁 JNDI课件
💻 JAVA~100~
字号:
/*曾海 2003/9 JNDI搜索程序改进版本本程序和searchLdap.java不同之处在于,它有选择地显示部份字段显示出来,省得看不清楚*/package jndi;import java.util.*;// hashtableimport javax.naming.*;//命名服务import javax.naming.directory.*;//初始化上下文用/* sunone: 50028本程序的名字系统结构是第一层 我的domainName就是我的主机,ddn-00887第二层 我的用户名是clyde/mirthrandir等第三层 cn就是全名组织全部是SVC*/public class showSomeAttr {  //如果你找不到相应的类名,就用Search-Search Classes来找,不会有错了 public static String sunLdapContext="com.sun.jndi.ldap.LdapCtxFactory"; public static String hostURL="ldap://127.0.0.1:50028";//别用localhost ,有时候不对劲儿 public static String searchBase="dc=jssvc , dc=com ";//从默认的点开始寻找,这里的根是jssvc.com public static String searchContents="uid=*";//sn=surname名字(sn=zeng)也可以,大家可以试试 public static String[] attrArray={ "sn","cn"};//定义要显示的ldap字段,这里是两个。你愿意定义多少个就定义多少个 public showSomeAttr() { try{    Hashtable env = new Hashtable();//准备放属性    env.put(Context.INITIAL_CONTEXT_FACTORY ,sunLdapContext);    env.put(Context.PROVIDER_URL ,hostURL);    SearchControls constraints  = new SearchControls();//这个类在naming.directory里的,用于确定搜索的范围,是全局,一层还是下一层,请看我的备课笔记    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE ) ;//搜索全部的目录树    DirContext  ctx = new InitialDirContext(env);//这句会有意外,好好捉吧    System.err.println("非常好,连接上了");    //搜索的结果是个Naming Enumeration集合对象,代表所有查到的用户    // 这个集合对象里有的是单个的SearchResult对象,一个对象一个用户,一个用户         //SearchResulut里面包括一个 dn 和一个 Attributes对象集合            //Attributes集合里每个元素是一个Attributes对象,代表一张用户信息表            //每个Attributes对象里是一堆的NamingEumeration,用getAll取得,针对用户的一行信息               //一个attributes对象里是一个enumeration,内容是某一项比如mail的多个值,头昏了吧。哈哈    NamingEnumeration results = ctx.search(searchBase,searchContents,constraints);    while(results!=null && results.hasMore() ){      SearchResult sr =(SearchResult)  results.next() ;//这句成功的      String  dn=sr.getName() ;      dn+=" , "+searchBase;      System.err.println("找到的记录标记是: "+dn );      //Attributes attrs=ctx.getAttributes(dn,attrArray) ;//直接根据dn取属性,在imail里就是取不到,昏特      //大问题啊,是imail不对?好象imail的层次结构是针对mail的,不完全是真的ldap      //03/9/11日的问题     Attributes attrs=ctx.getAttributes(dn,attrArray) ;//改用sunone了,再试试,就取到了!正确!imail 的问题      //Attributes attrs = sr.getAttributes() ;这句肯定对      if (attrs==null){      System.err.println("字段不存在");      }      else{         for(int k=0;k< attrArray.length ;k++){           Attribute item = (Attribute) attrs.get(attrArray[k]);           System.err.println("属性名:"+attrArray[k]+"的值集是");           Enumeration enum = item.getAll() ;           while(enum!=null && enum.hasMoreElements() )             System.err.print(enum.nextElement()+" **  " );           System.err.println() ;         }     }//else   }//外层扫描多条用户记录的循环  }catch (Exception e){ System.err.println("错误,连接不上服务器") ; System.exit(1);};  }  public static void main(String[] args) {    showSomeAttr showSomeAttr1 = new showSomeAttr();    System.exit(0);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -