Setting up Tomcat with OpenEJB On Window's 2000

|  |  |  | OpenEJB OpenJMS OpenORB Castor Tyrex |

Main
Welcome!
Download
Mailing Lists
The Team

Users
Quickstart
Hello World!
CMP Example
CMP Guide
Deploy
Startup
Validation
Configuration
Support
Request Feature
FAQ

Servers
Local Server
Remote Server
Tomcat

Integrators
Why OpenEJB
Overview
Design
Specification
Presentation

Developers
Custom Services
Release Plan
Source Code
SourceForge

Setting up Tomcat with OpenEJB On Window's 2000

Notes
Before we start
Installing Everything
Install Tomcat
Install OpenEJB
Hook them together
Restart Tomcat
Example Servlet
What if it Didn't Work?

## Notes

This is a rough draft of how to set up Tomcat with OpenEJB on Window's 2000. These are the minimum steps for integrating OpenEJB into Tomcat. The commands in this document are specifically for a Window's DOS prompt and Window's environment. Please feel free to add to this document or email the openejb-user mailing list with your suggetions. Your suggestions are always welcome!

## Before we start

We always tell users to send us info on their OS and what version of OpenEJB, Java, or any other programs they are using when they submit support requests. So, I guess it's only fair I do the same.

Here is some information about my setup

Here's the version of Window's and Java that I'm using:

    Microsoft Windows 2000 [Version 5.00.2195]
    (C) Copyright 1985-2000 Microsoft Corp.

    C:\>java -version
    java version "1.4.1-rc"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-rc-b19)
    Java HotSpot(TM) Client VM (build 1.4.1-rc-b19, mixed mode)

## Installing Everything

### Install Tomcat

If you don't already have Tomcat on your machine, download the Tomcat version of your choice from Apache.

http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/
Go into the latest version and click on the bin directory. If you have version 1.4.x of the Java 2 SDK you could download: jakarta-tomcat-4.x.x-LE-jdk14.exe - which is a lighter version for 1.4.x and makes use of the libraries that version 1.4.x has. Otherwise download jakarta-tomcat-4.x.x.exe which has the libraries that all versions below Java 2 SDK 1.4.x are missing.

NOTE

As an alternative to the self executable Tomcat download for Window's, you may also download the zip version named: jakarta-tomcat-4.x.x.zip or jakarta-tomcat-4.x.x-LE-jdk14.zip. Unzip this version into a directory instead of installing via the Window's installer. The main difference between the two is that the zip version does not have an NT Service installer or Start menu additions, so you would have to start/stop Tomcat manually.
If you choose to download the zip version of Tomcat, unzip it to a directory that makes sense to you (such as C:\) and follow the post Tomcat install directions. The start/stop Tomcat utility will not be in Start->Programs->Apache Tomcat 4.1, instead they will be in C:\tomcat-4.x.x\bin and you will need to run them manually by double clicking on them. The rest of the Tomcat directory structure will be the same in both versions.

Install Tomcat

Go to the directory you downloaded and double click jakarta-tomcat-xxx.exe. An install shield will pop up and ask you for several options. You may wish to click the check box that installs Tomcat as an NT service.

Once Tomcat is finished installing, it will automatically start up and you can test it by visiting this link: http://localhost:8080. If there are any problems, consult the Tomcat documentation on their website.

### Install OpenEJB

You need OpenEJB 0.9.0 or higher, any older versions of OpenEJB will not work. You can get that here: http://openejb.sf.net/download.html - download the zip version of OpenEJB. If you do not have a zip utility, you can get WinZip here: http://download.com.com/3000-2250-10161502.html

Unpack OpenEJB

Open your zip utility and unpack OpenEJB into the C:\ folder, or one that makes sense to you. For our examples, we'll unpack it into c:\openejb-0.9.0

### Hook them together

Go to the Tomcat Dir. In our case it should be: C:\Program Files\Apache Group\Tomcat 4.1

Add the OpenEJB Loader to Tomcat

We'll want to copy openejb_loader-0.9.0.war from the OpenEJB dist directory to the Tomcat webapps directory.

If you add openejb_loader-0.9.0.war to the webapps directory while Tomcat is running, it should automatically unpack the file and create a openejb_loader-0.9.0 folder. If it doesn't automatically extract, check the server.xml file that comes with Tomcat.

Now, let the OpenEJB Loader webapp know where to find your OpenEJB distribution by setting the openejb.home init-param.

Open the web.xml file in the directory webapps\openejb_loader-0.9.0\WEB-INF. Uncomment the openejb.home init-param then change the value from the default value to the actual path of your OpenEJB home directory.

### Restart Tomcat

Now, stop Tomcat and then start it again.

At this point, you're actually finished. All the libraries you need are in place.

Your servlets should use the following InitialContextFactory when creating an InitialContext to lookup beans

    org.openejb.client.LocalInitialContextFactory

The openejb.home is taken care of for us with the OpenEJB Loader webapp , so no other JNDI options are needed by your servlets to get an InitialContext from OpenEJB

## Example Servlet

You can use the OpenEJB Hello World as an example EJB to test things out. You won't need to do anything differently. Deploy the myHelloEjb.jar just as described.

http://openejb.sourceforge.net/hello-world.html

Once deployed, the myHelloEjb.jar should be in the beans directory.

    C:\openejb-0.9.0\beans\myHelloEjb.jar

You can use the HelloOpenEJB servlet below as an example of a servlet that looks up beans from OpenEJB.

It uses the HelloBean created in the OpenEJB Hello World document, so you'll need to run through that first. You can put the servlet in the following webapps directory

    C:\Program Files\Apache Group\Tomcat 4.1\webapps\examples\WEB-INF\classes

HelloOpenEJB.java

    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.naming.*;
    import javax.rmi.PortableRemoteObject;

    import org.acme.*;

    public class HelloOpenEJB extends HttpServlet {

        String factory = "org.openejb.client.LocalInitialContextFactory";

        public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        try{

            Properties p = new Properties();

            p.put(Context.INITIAL_CONTEXT_FACTORY, factory );
            InitialContext ctx = new InitialContext( p );

            //Lookup the bean using it's deployment id
            Object obj = ctx.lookup("/Hello");

            HelloHome ejbHome = (HelloHome)
                                 PortableRemoteObject.narrow(obj, HelloHome.class);

            //Use the HelloHome to create a HelloObject
            HelloObject ejbObject = ejbHome.create();

            //The part we've all been wainting for...

            out.println("<html>");
            out.println("<body>");
            out.println("<head>");
            out.println("<title>Hello World!</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>"+ ejbObject.sayHello() +"</h1>");
            out.println("</body>");
            out.println("</html>");
            } catch (Exception e){
                response.setContentType("text/plain");
                e.printStackTrace(out);
            }

        }
    }

Now, pop open your browser and go to the following URL.

    http://localhost:8080/examples/servlet/HelloOpenEJB

Should say "Hello World!!!" on the screen

## What if it Didn't Work?

1. Try re-starting Tomcat. The war file that was copied or the environment variable that we set may not have gotten picked up by Tomcat.
2. Make sure the openejb.home init-param in the web.xml file points to where you installed OpenEJB.
3. Check C:\Program Files\Apache Group\Tomcat 4.1 to see if there is an openejb.log file.
4. Send an email to the email list openejb-user@lists.sourceforge.net and give a thorough explain the problem.

Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. XML, XML Schema, XSLT and related standards are trademarks or registered trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web Consortium. All other product names mentioned herein are trademarks of their respective owners.