February 3, 2011

Part 3: Creating the ModelingAgency project in Eclipse

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

  1. Open up Eclipse. Under the Project Explorer tab right click and select New -> Project
  2. Select the Dynamic Web Project and click Next
  3. 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)
  4. 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

  • com.modelingagency.db
  • com.modelingagency.listeners
  • com.modelingagency.objects
  • com.modelingagency.servlets

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"
               username="test" password="test" 
      validationQuery="SELECT 1" /> 

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">
      <description>DB Connection</description>

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">
 <title>Modeling Agency Index</title>

Things are looking good.


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.

If all is well, the server should be running in Eclipse and you will see this:

Your project layout at this point should be looking like this:

Now you are ready to move on to

Part 4: Creating a Test Servlet