If you've followed the previous tutorials Part 1 and Part 2 you should now be ready to create a simple java web application called ModelingAgency in Eclipse, in order to learn how MySQL, MyBatis and Tomcat work together.
Creating a new project
- Open up Eclipse. Under the Project Explorer tab right click and select New -> Project
- Select the Dynamic Web Project and click Next
- Enter the project properties. For this project we will use the 2.4 Dynamic Web Module version, and the Tomcat 6.0 Runtime (which you may have to configure manually)
Click Finish. You should now have a project that looks like this:
Setup the java packages
To organize our code a little, let's create the following packages under our /src directory
Click on Java Resources: src -> New -> Package and create the packages above
External jar files
Remember all those jar files you downloaded in Part 1 (the mybatis jar, standard.jar,jstl.jar,etc). Now it's time to make them available to the webapp by placing them under the /WEB-INF/lib folder.
You can download all the jar files you need to place in this folder here.
Configuring the database connection
In order to connect to the database we have setup in Part 2, we have to add a context.xml file under the /META-INF folder.
There are other was to setup a database connection from Tomcat, but placing it in /META-INF/context.xml allows each webapp to be bundled with its database configuration, and is easier to manage in my experience.
Create the following file and place it under /META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?> <Context docBase="ModelingAgency" path="/ModelingAgency" reloadable="true" > <!-- http://commons.apache.org/dbcp/configuration.html --> <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="5" maxWait="10000" username="test" password="test" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/modeling_agency" removeAbandoned="true" removeAbandonedTimeout="10" logAbandoned="true" validationQuery="SELECT 1" /> </Context>
Note:The docBase attribute and path map to the same name as your Eclipse project name. Also, If you have a different username or password that you created in Part 2, other than 'test' make sure to use it instead.
The deployment descriptor, web.xml, is the most important piece in any Java webapp (but I'm sure you know that already).
In order to make the database connection we defined in context.xml known to your webapp, you should also add a resource entry in web.xml.
Edit your web.xml file so it looks like this:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>ModelingAgency</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/mydb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
Starting the app
Just to make sure we're on the right track and things are working fine, let's create an index page and start our server:
First, create the following index.jsp file under the /WebContent folder.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Modeling Agency Index</title> </head> <body> Things are looking good. </body> </html>
Now to start the ModelingAgency web application on a tomcat server,
right click the index.jsp file, select Run As -> Run on server
Make sure the Server's host name is localhost and the Server runtime environment is associated with your Tomcat 6.0 installation. Click Finish.
Now you are ready to move on to