【问题标题】:Is it possible to list the avaiable JNDI datasources?是否可以列出可用的 JNDI 数据源?
【发布时间】:2013-10-03 11:22:30
【问题描述】:

是否可以列出当前应用程序的可用 JNDI 数据源?如果是,我该怎么做。

【问题讨论】:

  • 对于网络应用程序?
  • 如果是,在哪个应用服务器上?
  • @MaVRoSCy 最好是独立于应用程序服务器。这是一个图书馆。但首先我们需要它用于 WebSphere。

标签: jdbc datasource jndi


【解决方案1】:

下面是一些在 Servlet 中尝试的示例代码:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    try {
        InitialContext ictx = new InitialContext();
        Context ctx = (Context) ictx.lookup("java:");
        out.println("java: = " + ctx.getClass().getName());
        printContext(out, ctx, 1);
    } catch (Exception exc) {
        throw new ServletException(exc);
    }
}

private void printContext(PrintWriter out, Context ctx, int indent) throws ServletException, IOException, NamingException {
    NamingEnumeration en = ctx.listBindings("");
    while (en.hasMore()) {
        Binding b = (Binding) en.next();
        char[] tabs = new char[indent];
        Arrays.fill(tabs, '\t');
        out.println(new String(tabs) + b.getName() + " = " + b.getClassName());
        try {
            if (b.getObject() instanceof Context) {
                printContext(out, (Context) b.getObject(), indent + 1);
            }
        } catch (Exception exc) {
            throw new ServletException(exc);
        }
    }
}

尝试一下,如果它有效,请告诉我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2016-10-11
    • 2012-12-17
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多