I’m developing a simple project.In this project i’m using n no of jsp pages and servlets to interact with the database. How i have to write the connection string. Presently i written the connection string each and every jsp page.(I knew this is not a best practice). Now my question is how i have to manage the connection string? I have to write in only one common page, then i have to utilize that.. How i can implement this ?? Could any one guide me on this??
Share
You need to create a JNDI datasource in the servletcontainer. It is by default a connection pooled datasource already. How to do that depends on the servletcontainer make/version. So here’s just a Tomcat targeted example:
First create a file
/META-INF/context.xml(to be clear, the META-INF is at the same level as the WEB-INF of the webapp) and fill it with the following (assuming a MySQL DB).Then register it in your webapp’s
/WEB-INF/web.xml.Get it as follows in your DB manager / DAO class.
Finally get the connection of it inside the DAO method where you’re executing the query.
Don’t forget to
close()it inside thefinallyof thetrywhere you’re getting it.See also: