views:

48

answers:

2

I'm using Tomcat 6.0, and I want to know how can I configure Tomcat's server.xml file to connect to mysql database, and enable form based authentication in java.

I'm currently using mysql 5.1, and I've already downloaded mysql connector jar file, and put in lib directory of Tomcat.

+2  A: 

Typically context.xml and server.xml are separated, and you usually configure a data source on web-app level, that is in the context of that web app. The reason for that is that a data source connects not so much to a server but to a database within that server, and having multiple apps accessing the same database is not always a good idea (if you didn't design the apps for that).

That said, have a look at this tomcat wiki page which describes what you want (or what I think you want).

For authentication check out this thread on velocity reviews.

extraneon
+3  A: 

I'm guessing you want Tomcat to create connection pool to MySQL database. In that case, you don't need to configure server.xml file. In the context.xml file you need to add a <Resource> element, something like this:

<Resource name="jdbc/MySQLPool" auth="Container" type="javax.sql.DataSource"
          factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
          maxActive="100" maxIdle="30" maxWait="10000"
          username="USERNAME" password="PASSWORD"
          driverClassName="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"
          url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=Cp1251"
          removeAbandoned="true" />

(I'm not sure if that's the correct driverClassName for MySQL, but your Resource should look somewhat like this).

For more info, try checking Tomcat's documentation on JNDI Resources and JDBC DataSources.

Ivan Vrtarić