EJB 3.1 in a WAR file using Glassfish v3

March 10th, 2009  | Tags: , , ,

The next Java EE release, version 6, is going to bring us many news. Among them, EJB 3.1 seems the most interesting, in my opinion.

Although it’s a draft yet, we can make some testing using Glassfish V3. To get started, we’re going to show up how to create a single Stateless Session Bean (without interface) and package it in a WAR file. That’s right!! Stateless without interface and package it in a WAR file. It belongs to EJB lite. Depends on the profile you’re going to use, the EJB Module is no longer used. Roberto Chinnici’s blog gives you a better explanation about profiles in Java EE 6. Check it out.

Installing Glassfish and adding support to Java EE 6

As I said, we’re able to make some testing in Glassfish V3. Because of this, let’s install Glassfish and add support for EJB 3.1 on it.

First of all, download the glassfish here. Make sure to download the proper version: GlassFish Server v3 Prelude.

Note: If you’re going to use Netbeans 6.5, Glassfish comes by default in its instalation, hence, you do not need to download it separately.

After the Glassfish v3 has been installed, it’s time to add support for EJB 3.1. In general speaking, there are two steps to go through:

Install the Glassfish’s Update tool:

To do that, go to GLASSFISH_DIR/bin and run the updatetool tool. In the first time, you’ll get a message like the image below:

The rest of the process is straightforward. After the tool is installed, run the updatetool again and a screen will come up.

Add EJB 3.1 support

On the Update Tool screen, make the following steps:

  1. From the left panel, pick the Available add-ons
  2. Click the glassfishv3-ejb checkbox and click install
  3. Click the glassfishv3-ejb checkbox and click install


It’s done. It was easy, wasn’t it?

Creating the Project

I’m going to use Netbeans 6.5 to test EJB 3.1, but you’re free to use another IDE or even a text editor.

As I already said, we’re going to package the EJB in a WAR file, hence, we’re going to create a simple Web Project. No EAR and EJB module are going to be used, that’s nice :).

To do that using Netbeans, go to menu File -> New Project -> Java Web -> Web Application. My web project’s name is TestEJB31.

Coding the Stateless Session Bean Component

Time to have fun. Let’s create a single Stateless Session Bean in our web project. The code looks like below:

package com.jairrillo.sessionbeans;
 
import javax.ejb.Stateless;
 
/**
 *
 * @author Jair Rillo Junior (jrjuniorsp@yahoo.com.br)
 */
@Stateless
public class HelloWorldEJB {
 
    public String sayHello() {
        return "Hello World EJB";
    }
}

Wait a moment! The code above looks like a simple POJO, except by the annotation @Stateless.

You’re right! The EJB 3.1 makes our life easier. No interface (at least for Local Session Beans), no inheritance, no xml mapping. EJB 3.1 is awesome, isn’t it?

Creating a Servlet to call the EJB 3.1 Session Bean Component

We need a client to call our EJB component. The best way to do that is create a Servlet. Let’s create a servlet with the name: TestServlet. The code looks like below:

package com.jairrillo.servlets;
 
import com.jairrillo.sessionbeans.HelloWorldEJB;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 *
 * @author jairrj
 */
public class TestServlet extends HttpServlet {
 
    @EJB
    private HelloWorldEJB helloWorldEJB;
 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TestServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>" + helloWorldEJB.sayHello() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally { 
            out.close();
        }
    }
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 
}

Nothing special, huh? Servlet calls the EJB component and prints out the result in the output object.

Running the EJB 3.1 in a Web Project (WAR file)

Start up the Glassfish and deploy the WAR on it. After that, you can perform the Servlet using the URL: http://localhost:8080/TestEJB31/TestServlet. In the Netbeans 6.5, you can do that clicking on the TestServlet.java class and pressing the Shift + F6 buttons.

When you run the Servlet, you’ll get a screen with the phrase: Hello World EJB. It means the EJB 3 component worked fine in a WAR file.

Conclusion

Java EE 6 will bring us many new features. Among them, I think EJB 3.1 will be the most interesting. So far, we’re able to make some real testing in EJB 3.1 using Glassfish v3. In this topic, I showed you up how to do that.
EJB 3.1, as well as the whole Java EE specification is becoming more interesting and straightforward. If you get used to say that EJB is complicated, it’s time to chance your mind :).

I hope this topic be useful for anyone. See you in the next post.

  1. Leonardo Carreira
    July 22nd, 2009 at 22:26
    #1

    Thanks for your post.. :)
    it’s very useful for me.. :)

    i have one question..
    How to access EJB3.1 from remote client?..
    as we know that in EJB3 we uses Remote Interface..

    Thanks in advance.. :)

  2. July 23rd, 2009 at 12:36
    #2

    Hi Leonardo,

    EJB 3.1 has two flavours: EJB Lite and EJB Full. In this post I’ve showed the EJB 3.1 lite, which does not have interfaces and works only for LOCAL calls. If you need remote interfaces, you must use EJB 3.1 full and the process works similar than version 3.0.
    I hope this information be useful.

TOP