Do it once in a ServletContextListener
instead of everytime in init()
of many servlets. The contextInitialized()
method is executed only once during webapp's startup.
public class Config implements ServletContextListener {
private static final String ATTRIBUTE_NAME = "config";
private DataSource dataSource;
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
String databaseName = servletContext.getInitParameter("database.name");
try {
dataSource = (DataSource) new InitialContext().lookup(databaseName);
} catch (NamingException e) {
throw new RuntimeException("Config failed: datasource not found", e);
}
servletContext.setAttribute(ATTRIBUTE_NAME, this);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// NOOP.
}
public DataSource getDataSource() {
return dataSource;
}
public static Config getInstance(ServletContext servletContext) {
return (Config) servletContext.getAttribute(ATTRIBUTE_NAME);
}
}
Configure it as follows in web.xml
:
<context-param>
<param-name>database.name</param-name>
<param-value>jdbc/mysqldb</param-value>
</context-param>
<listener>
<listener-class>com.example.Config</listener-class>
</listener>
You can obtain it in your servlet as follows (init()
or doXXX()
method, you choose):
DataSource dataSource = Config.getInstance(getServletContext()).getDataSource();
I'd however refactor it a step further, JDBC code should preferably be placed in its own classes, not in servlets. Lookup the DAO pattern.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…