Resin: The Instant Application Server
Deploying A Test Application
So you now have a robust Web application server up and running,
but what can it actually do for you? In this section, we'll deploy a simple test
application into our server, getting you started and giving you an overview of
how Resin's most important features can make your Web development easier.
We will create a basic example of how one can use the Container-Manager
Persistence (CMP) features of Resin to simplify and standardize the communication
between a Java servlet and a MySQL database. For this, you also need to have
a MySQL server running (or any other database server for which you have
JDBC drivers). Execute the following SQL queries to create a test table and
populate it with a few rows:
use test;
CREATE TABLE turtle
(
turtle_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
turtle_name VARCHAR(50),
PRIMARY KEY(turtle_id)
);
INSERT INTO turtle VALUES(NULL,"Donatello");
INSERT INTO turtle VALUES(NULL,"Michaelangelo");
INSERT INTO turtle VALUES(NULL,"Raphael");
INSERT INTO turtle VALUES(NULL,"Leonardo");
As you can see, this table will be used by a turtle enthusiast
for keeping track of his beloved creatures.
For the representation of the table and its rows, we will create
a total of three classes:
Turtle: represents a single row (a turtle) in the table.
TurtleHome: represents the whole table (with functions for searching
and receiving rows/turtles).
TurtleBean: holds the business logic and serves as the link
between the interfaces (Turtle and TurtleHome) and the servlet.
These three clases are listed below. (Tip: It would be wise to turn off Resin while fiddling with these files. Since Resin tries to compile and reload configuration changes on-the-fly, it's possible
you'll get lots of annoying error messages on the screen while working.)
Example 1. Turtle.java.
package org.solin.web.Turtle;
import javax.ejb.*;
public interface Turtle extends EJBLocalObject
{
String getTurtleId();
String getTurtleName();
}
Example 2. TurtleHome.java.
package org.solin.web.Turtle;
import javax.ejb.*;
import java.util.Collection;
public interface TurtleHome extends EJBLocalHome
{
Turtle findByPrimaryKey(Integer name)
throws FinderException;
Collection findAll()
throws FinderException;
}
Example 3. TurtleBean.java.
package org.solin.web.Turtle;
import java.util.Collection;
public abstract class TurtleBean extends com.caucho.ejb.AbstractEntityBean
{
public abstract String getTurtleId();
public abstract String getTurtleName();
}
Basically, you can see here that TurtleHome
will give us rows (turtles) from the table in the form of Turtle objects, and TurtleBean is the entity bean that will be used by the servlet to hold the actual data.