问题描述
我需要协助。 我必须将整个jndi db选项检索到html文件中的选择列表,这意味着我需要访问所有jndi db名称并获取这些值。
1楼
在其中一台WebLogic服务器中查找JNDI名称时遇到了一些麻烦。 我创建了一种遍历JDNI树结构的方法,并将所有这些方法打印到控制台。 我不确定这是否是您所需要的,但这是我使用的代码:
private void printList(String directory){
try {
NamingEnumeration<NameClassPair> namings = context.list(directory);
System.out.println("************Printing " + directory + "**************");
while(namings.hasMoreElements()){
if(directory.equals("")) printList(namings.next().getName());
else printList(directory+"."+namings.next().getName());
}
System.out.println("Done printing " + directory);
} catch (NamingException e) {
// TODO Auto-generated catch block
System.out.println(directory);
}
}
如果要从根目录访问所有元素,则可以传递所需目录的字符串,也可以传递空字符串。
您需要导入javax.naming.*
(或-NamingEnumeration
/ -NameClassPair
)
差点忘了:您首先需要定义一个上下文,该上下文应用于查找目录的方法:
Context context = null;
Hashtable<String, String> ht = new Hashtable<String, String>();
ht.put(Context.PROVIDER_URL, "your_host");
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
//this above needs to be changed to fit your desired system
ht.put(Context.SECURITY_PRINCIPAL, "your_user");
ht.put(Context.SECURITY_CREDENTIALS, "your_password");
try {
context = new InitialContext(ht);
// Use the context in your program
}
catch (NamingException e) {
e.printStackTrace();
}