ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Resin: The Instant Application Server
Pages: 1, 2, 3

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-ql>SELECT o FROM turtle o</ejb-ql>

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.

    <init-param user="root"/>
    <init-param password="secret"/>
    <init-param data-source="java:comp/env/jdbc/test"/>

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
         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();
      out.println("<h2>My Turtles</h2>");
      out.println("Below is a listing of my turtles, 
                   in no specific order.<br><br>");
          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:

  <servlet-mapping url-pattern='/turtle' servlet-name='turtle-servlet'/>
  <servlet servlet-name='turtle-servlet' 
    <init-param greeting='My turtles'/>

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.


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.

Apache Pocket Ref

Related Reading

Apache Pocket Ref
By Andrew Ford

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.