I have the following resource tag in my context.xml file:
type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="1000"
username="user" password="passwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDB" />
I am developing a Java web app using the Stripes framework in NetBeans.
How can I get this resource from within a Java class?
解决方案
You need your bean to be instantiated by something (a dependecy injection framework) which knows how to handle the @Resrouce annotation. JSP itself doesn't know how.
In this case it would be simpler to locate the DataSource in the JNDI context:
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/myDS");