How to Create a Simple EJB Project

EJBs are specialized Java components that help you build enterprise applications quickly.

How to Create a Simple EJB Project

When we create enterprise software, we need to make sure it can handle massive scale, system failures, security concerns, and more.

But if we have to design these services manually, it will slow down development.

We want to get enterprise functionality and still build our application quickly.

And to do that, we need to use EJBs (Enterprise Java Beans).

So..

Why do we use EJBs?

EJBs help us build enterprise applications quickly. They automatically take care of lower level details like:

  1. Load-balancing & Failover
  2. Transaction Management & Persistence
  3. Security

EJBs are special java components that run inside a container. And this container handles the services mentioned above.

How do we use EJBs?

What You'll Need

Create an EJB Project

We’ll create a project that calls an EJB from a servlet.

Step 1: Add a Maven Repository.

  • Go to JBoss Developer Studio (or Window) -> Preferences
  • Type jboss maven integration -> Click Configure Maven Repositories
  • Click Add Repository and fill in the following
Profile ID: jboss-ga-repository 
ID: jboss-ga-repository 
Name: jboss-ga-repository 
URL: http://maven.repository.redhat.com/techpreview/all 
Active by default: Enabled

Step 2: Add Maven dependencies

  • In the Dependencies tab of your pom.xml, click Add
  • Add jboss-ejb-api_3.1_spec & jboss-servlet-api_3.0_spec

Step 3: Change maven packaging and properties

  • In your pom.xml, change the packaging to WAR.
<packaging>war</packaging>
  • Update the maven-war-plugin. Add this before the </project> tag
<build> 
    <plugins> 
        <plugin> 
            <artifactId>maven-war-plugin</artifactId> 
            <version>3.0.0</version> 
        </plugin> 
    </plugins> 
</build>

Step 4: Update Maven

Step 5: Create a Stateless EJB

  • Create a new class called MyEJB
  • Add @Stateless annotation
@Stateless public class MyEJB
  • Add a helloWorld method
public String helloWorld(String name) {     
	return "Hello " + name;
}

Step 6: Create a Servlet

  • Create a class called SimpleServlet that extends HttpServlet
  • Add @WebServlet annotation
@WebServlet("/SimpleEJB") 
public class SimpleServlet extends HttpServlet
  • Declare an EJB and add a doGet method that calls it
@EJB
MyEJB ejb;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{

    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<body>");
    out.println("<h1>" + ejb.helloWorld("yourname") + "</h1>");
    out.println("</body>");
    out.println("</html>");
}

Step 7: Add JBoss Web descriptor

  • In the /src/main/webapp/WEB-INF directory, create a file called jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>     
    <context-root>simple-ejb-project</context-root> 
</jboss-web>

Step 8: Run on server

Step 9: You should see a successful EJB and web deployment in the Console

java:global/simple-ejb-project-0.0.1-SNAPSHOT/MyEJB!org.codelikethewind.simpleejb.MyEJB 
... 
Registered web context: /simple-ejb-project

Step 10: Point your browser to http://localhost:8080/simple-ejb-project/SimpleEJB

You should see "Hello World!" Let's review what we just did.

Recap

Maven (Steps 1-4)

  • We added the necessary APIs for EJBs and servlets.
  • We changed the packaging to WAR (Web ARtifact) since we're making a web application
  • Servlet applications (before version 3.0) must have a web.xml file. We updated our version to 3.0 so we can skip that file. Otherwise you'll see this error:web.xml is missing and is set to true

Stateless EJB (5)

  • We created an EJB using the @Stateless annotation
  • Stateless EJBs have no instance variables.

Servlet (6)

  • We created a servlet, a java class that responds to an HTTP request
  • We mapped our servlet to a path (/SimpleEJB) with the @WebServlet annotation
  • We declared our EJB using the @EJB annotation and called helloWorld()

Note: Since the container handles instantiation of our EJB, we don't need to use the new keyword

JBoss Web Descriptor (7)

We changed the context root so we can access our servlet here:

http://localhost:8080/simple-ejb-project

instead of the default

http://localhost:8080/simple-ejb-project-0.0.1-SNAPSHOT

What's a best practice for using EJBs?

It's a good practice to separate business logic (EJBs) from view logic (Servlets etc). This makes it easy to reuse business logic in other parts of your application.

But for small projects like this one, sometimes that is unnecessary.

So..

  1. EJBs help us build enterprise applications quickly because they automatically handle massive scale, failure recovery, security, and more.
  2. We use annotations to define and declare EJBs
  3. Separate your business (EJB) and view logic (Servlet) for easy code re-use
  1. Project Source Code
  2. A full history of  EJBs
  3. The difference between a JAR, WAR, EAR

Next time we'll look at other ways to invoke EJBs

Happy Coding,

-T.O.