1: Where to place mysql-connector-java-5.1.13-bin
in Tomcat directory? Should I place it under Tomcat 6.0\webapps\myapp\WEB-INF\lib
?
That depends on where the connections are to be managed. Normally you would like to create a connection pooled JNDI datasource to improve connecting performance. In that case, Tomcat is managing the connections and need to have access to the JDBC driver. You should then drop the JAR file in Tomcat/lib
.
But if you're doing it the basic way using DriverManager#getConnection()
, then it in fact don't matter if you drop it in Tomcat/lib
or YourApp/WEB-INF/lib
. You however need to realize that the one in Tomcat/lib
will apply for all deployed webapps and that the one in YourApp/WEB-INF/lib
will override the one in Tomcat/lib
for only the particular webapp.
2: Do I need to confirgure context.xml
or server.xml
files?
That depends on where the connections are to be managed. When using a JNDI datasource, it suffices to configure it using YourApp/META-INF/context.xml
like follows (just create file if not exist):
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/yourdb" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/yourdb"
driverClassName="com.mysql.jdbc.Driver"
username="yourname" password="yourpass"
/>
</Context>
and the YourApp/WEB-INF/web.xml
as follows:
<resource-env-ref>
<resource-env-ref-name>jdbc/yourdb</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
If you're doing it the basic DriverManager
way, then it's all up to you. Hardcoded, properties file, XML file, etcetera. You should manage it youself. Tomcat won't (and can't) do anything useful for you.
Noted should be that the YourApp/META-INF/context.xml
is specific to Tomcat and clones. Each servletcontainer/appserver has its own way of defining JNDI resources. In Glassfish for example, you'd like to do that through the webbased admin interface.
3: Should I write web.xml
file and need to place under Tomcat 6.0\webapps\myapp\WEB-INF
? If Yes, then what should be the contents of file?
You should always supply one. It's not only to configure resources, but also to define servlets, filters, listeners and that kind of mandatory stuff to run your webapp. This file is part of the standard Servlet API.
See also: