Resin: The Instant Application Server
These files should be placed in the WEB-INF/classes subdirectory of your
document root (default is <RESIN_HOME>/docs). For our entity bean (TurtleBean) to work, we need to create a deployment descriptor that describes the bean. Place the
turtle.ejb file (shown in Example 4) in your WEB-INF directory:
Example 4. turtle.ejb.
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>TurtleBean</ejb-name>
<local-home>org.solin.web.Turtle.TurtleHome</local-home>
<local>org.solin.web.Turtle.Turtle</local>
<ejb-class>org.solin.web.Turtle.TurtleBean</ejb-class>
<prim-key-class>Integer</prim-key-class>
<primkey-field>turtleId</primkey-field>
<persistence-type>Container</persistence-type>
<reentrant>True</reentrant>
<abstract-schema-name>turtle</abstract-schema-name>
<sql-table>turtle</sql-table>
<cmp-field>
<field-name>turtleId</field-name>
</cmp-field>
<cmp-field>
<field-name>turtleName</field-name>
</cmp-field>
<query>
<query-method>
<method-name>findAll</method-name>
</query-method>
<ejb-ql>SELECT o FROM turtle o</ejb-ql>
</query>
</entity>
</enterprise-beans>
</ejb-jar>
In this file, we define the classes
that are used as interfaces, and by what class the bean is represented. We also
define what fields we want to be able to access, and a query for retrieving all
rows in the table (defined in TurtleHome.findAll()). As usual with EJB, we
also need to attach the bean to Resin with the web.xml
file (located in WEB-INF). The code for doing this is listed in Example 5.
Example 5. web.xml.
<web-app>
<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.XADataSource</res-type>
<driver-name>com.caucho.jdbc.mysql.Driver</driver-name>
<url>jdbc:mysql_caucho://localhost:3306/test</url>
<init-param user="root"/>
<init-param password="secret"/>
</resource-ref>
<resource-ref>
<res-ref-name>java:comp/env/cmp</res-ref-name>
<class-name>com.caucho.ejb.EJBServer</class-name>
<init-param data-source="java:comp/env/jdbc/test"/>
</resource-ref>
</web-app>
In this example, we use the
MySQL driver that comes with Resin, but you can, of course, change these
parameters to better fit your setup. Finally, we also need a servlet to
communicate with your browser. See Example 6 below.
Example 6. TurtleServlet.java.
package org.solin.web.Turtle;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.ejb.*;
public class TurtleServlet extends HttpServlet
{
private TurtleHome turtlehome = null;
public void init() throws ServletException
{
try
{
Context cmp = (Context) new InitialContext().lookup("java:comp/env/cmp");
turtlehome = (TurtleHome) cmp.lookup("TurtleBean");
}
catch(NamingException e)
{
throw new ServletException(e);
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("<h2>My Turtles</h2>");
out.println("Below is a listing of my turtles,
in no specific order.<br><br>");
try
{
Collection c = turtlehome.findAll();
Iterator iter = c.iterator();
while( iter.hasNext() )
{
Turtle turtle = (Turtle) iter.next();
out.println(turtle.getTurtleId() + ". " +
turtle.getTurtleName() + "<br>");
}
}
catch( Exception e)
{
throw new ServletException(e);
}
}
}
Resin also needs to know about the
servlet, so add the following lines to your
resin.conf:
<web-app>
<servlet-mapping url-pattern='/turtle' servlet-name='turtle-servlet'/>
<servlet servlet-name='turtle-servlet'
servlet-class='org.solin.web.Turtle.TurtleServlet'>
<init-param greeting='My turtles'/>
</servlet>
</web-app>
Note that this needs to reside within the <host> tag.
When all is done, start (or reload) Resin. All of your newly-added configuration
and source files will now be automatically compiled and registered by the
server. Actually, Resin checks for changes in the files all the time, and
automatically recompiles and/or reloads a source file or configuration file as
it gets updated. No more tedious server-restarting and manual recompiling!
OK, point your browser at http://localhost/turtle. It should show something similar to Figure 2.

Figure 2. Output of Turtles application.
Summary
You now have a working Resin installation and a running example
of a database-driven Web application based on EJB/CMP. When mentioning these
techniques, one usually thinks about heavily-loaded, large-scale sites, divided
into several servers, but Resin makes this useful to us mortals as well.
Rapid server installation and simplified database management is a good thing for everyone.
If you want to get deeper into Resin-CMP right away,
take a look at
this tutorial. As stated earlier, a very common setup is to use Resin
side-by-side with Apache, letting Resin deal exclusively with the Java requests. You can
find more information about that configuration and a few others
in this document.
When one has figured out the features and benefits of Resin,
development usually gets both more fun and faster. Good luck!
Daniel Solin
is a freelance writer and Linux consultant whose specialty is GUI programming. His first book, SAMS Teach Yourself Qt Programming in 24 hours, was published in May, 2000.
Return to ONJava.com.