Deploying EJB3 In Action code examples in NetBeans

I’m currently learning Enterprise Java Beans 3 using Debu Panda et al.’s book EJB3 in Action. The material is not very hard but there’s lots of stuff to memorize, and the best way to do that is by running the code examples and trying to change it (and break it). The easiest way to do that is by using an IDE, but the book isn’t very helpful in that area, and I didn’t find it really transparent or straightforward, so here’s a write up of what I did to make the code examples of the first chapter work in NetBeans.

You’ll need a NetBeans installation with most of the trimmings for this to work. Make sure GlassFish Server 3 and JavaDB are working.

The first chapter is about a simple Hello World application. To deploy it, we need an EJB3 module and a Web Application Module. The latter will be the client, replacing the client application provided by the book.

  1. In NetBeans, create a new project: Java EE -> Enterprise Application (click Next).
  2. Enter HelloUser as project name and click Next.
  3. Click Enable Context and Dependency Injection and click Finish.
  4. Download and unzip the example sources.
  5. Within the newly created directory, locate the directory glassfish/chapter1/src/ejb/bean/ejb3inaction, and copy it to HelloUser/HelloUser-ejb/src/java/ within your NetBeans project directory.
  6. From the example sources directory,  locate the directory glassfish/chapter1/src/ejb/client/ejb3inaction, and copy it to HelloUser/HelloUser-war/src/java/ within your NetBeans project directory.

The client class provided by the book is a very simple illustration of what has been explained so far:

import javax.ejb.EJB;

public class HelloUserServlet extends HttpServlet {

    private HelloUser bean;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        bean.sayHello("Curious George");

        PrintWriter out = response.getWriter();
        try {
            out.println("<title>Servlet HelloUserServlet</title>");
            out.println("Invoked EJB successfully .. see server console for output");
        } finally {

This is not something that I could run from inside NetBeans, so I wrote a servlet that contains the same code.

  1. In the Projects frame, expand HelloUser-war -> Source Packages -> ejb3inaction.example. This package was created when we copied the files to HelloUser-war, back in step 6.
  2. Right click the package node and click New Servlet…
  3. Use HelloUserServlet as class name and select Add information to deployment descriptor (web.xml). Click Finish.
  4. Replace everything between the import statements and the HttpServlet methods (they’re folded) with the following:

As you can see, it is very easy to create a servlet that does what the main method in the client class from the book does, by simply copying a few lines and removing a few unneeded ones from the default servlet created by NetBeans.

When you run the project, your web browser will come up but you won’t see the output yet. Direct it to You should see a single line of text. In NetBeans, go to the output window and select the GlassFish Server 3 tab. There should be a line starting with the word INFO, followed by the text from HelloUserBean.sayHello().

Now let’s see if this approach will work equally well for the rest of the examples in the book.

Categorised as: howto, java

One Comment

  1. Jeremy says:

    Thanks, That was a very good hint that save me more screwing around.