Java 从程序中的context.xml访问资源的属性
java中是否有方法从程序访问context.xml中给定的资源标记的值Java 从程序中的context.xml访问资源的属性,java,tomcat,Java,Tomcat,java中是否有方法从程序访问context.xml中给定的资源标记的值 <Context ...> ... <Resource name="jdbc/EmployeeDB" auth="Container" type="javax.sql.DataSource" username="dbusername" password="dbpassword" driverClassName="org.hsql.jdbcDriver
<Context ...>
...
<Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource" username="dbusername" password="dbpassword"
driverClassName="org.hsql.jdbcDriver" url="jdbc:HypersonicSQL:database"
maxActive="8" maxIdle="4"/>
...
</Context>
但是有没有办法在我在资源标签中给出的程序中打印用户名和密码?您可以尝试将其转换为资源的实际实现(使用.getClass().getName()打印名称),并使用其中提供的方法。例如,tomcat的BasicDataSource具有getPassword getUser方法
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();