1

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

4

1 回答 1

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);
        }
    }
}

试试看,让我知道它是否有效

于 2013-09-30T06:57:25.703 回答