Jdbc 可以列出可用的JNDI数据源吗?

Jdbc 可以列出可用的JNDI数据源吗?,jdbc,datasource,jndi,Jdbc,Datasource,Jndi,是否可以列出当前应用程序的可用JNDI数据源?如果是,我该怎么做。以下是一些在Servlet中尝试的示例代码: @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); PrintWriter out = r

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

以下是一些在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);
        }
    }
}

请尝试一下,并告诉我它是否有效

以下是一些在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);
        }
    }
}

尝试一下,让我知道它是否适用于web应用程序

?如果适用于哪个应用程序服务器?@MaVRoSCy最好是独立于应用程序服务器的。这是给图书馆的。但是首先我们需要它用于WebSphere。对于web应用程序?如果是,在哪个应用程序服务器中?@MaVRoSCy最好是独立于应用程序服务器的。这是给图书馆的。但首先,我们需要它用于WebSphere。