【问题标题】:How to get all JNDI db options?如何获取所有 JNDI 数据库选项?
【发布时间】:2015-07-23 10:47:23
【问题描述】:

我需要一些帮助。我必须将整个 jndi db 选项检索到 html 文件中的选择列表中,这意味着我需要访问所有 jndi db 名称并获取这些值 任何想法 ?

【问题讨论】:

    标签: java servlets jndi


    【解决方案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();
            }
    

    【讨论】:

    • 您不一定在所有容器中都这样做。 Servlet 规范 AFAIK 不需要它。
    • 我在这里回答可能有点为时过早。真的没有足够的信息继续下去。至少对我来说不是。感谢您的澄清。
    • @KjetilNordin ht.put(Context.PROVIDER_URL,"your_host");和其他 ht.put 给出错误。原因 Context.PRO... 不存在
    • 您必须将“your_host”替换为您主机的实际名称 (URL:port)。这也适用于您的用户和密码,以及初始上下文工厂,因为我不知道您在那里有什么样的设置。你还没告诉我们。
    猜你喜欢
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多