Java EE Programming/Stateless Session Beans

From Wikibooks, open books for an open world
Jump to: navigation, search

Here is a short tutorial to use a stateless session EJB using Eclipse.

  1. In Eclipse, right-click on the Project Explorer view.
  2. Select New -> EJB Project. If EJB Project doesn't appear, Select New -> Other, select EJB -> EJB Project and click on Next.
  3. On "Project name", type helloworld-ejb .
  4. Click on Finish.
  5. Right-click on the Project Explorer view.
  6. Select New -> Session bean (EJB 3.x). If Session bean (EJB 3.x) doesn't appear, Select New -> Other, select EJB -> Session bean (EJB 3.x) and click on Next.
  7. On "Java package", type org.wikibooks.en .
  8. On "Class name", type MyFirstEJB . Leave the other options as it is.
  9. Click on Finish.
  10. On the same package, create a new interface MyFirstEJBRemote .
  11. Add the following method signature inside it:
    public String sayHello();
  1. Add the following annotation above the signature of the interface:
@Remote
  1. Open the class MyFirstEJB.
  2. Remove the annotation @LocalBean .
  3. Add the following method inside it:
    public String sayHello() {
        return "Hello World!";
    }
  1. Right-click on the project.
  2. Select Export -> EJB JAR file . If you don't find the option EJB JAR file, click on Export... instead, select EJB -> EJB JAR file and click on Next >. The web project should be named helloworld .
  3. Choose a location for the destination.
  4. Click on Finish .
  5. Go on the folder where you have created your JAR.

You should see a JAR file named helloworld-ejb.jar . You can delete it.

  1. Right-click on the Project Explorer view.
  2. Select New -> Enterprise Application project.
  3. On "Project name", type helloworld-ear .
  4. Click on Next .
  5. Select the project helloworld-ejb .
  6. Click on Finish . You should have a new project called helloworld-ear. Among other things, it should contain Deployment Descriptor: helloworld-ear/Modules/EJB helloworld-ejb.jar .
  7. Right-click Export -> EAR file, choose a destination and click on Finish.
  8. Create a copy of the EAR file and change the extension to .zip .
  9. Explore the content of the ZIP file.

You should see the JAR file named helloworld-ejb.jar inside. You can delete the ZIP file.

  1. Copy/paste your EAR file in the deployment folder of your application server.
  2. Start your application server.

Now your EJB is usable. Unfortunately, we don't know how to use it yet.

  1. Shutdown your application server.
  2. Reuse the WAR project that you created in this page.
  3. Right-click on Java Resources/src .
  4. Select New -> Package .
  5. On name, type org.wikibooks.en .
  6. Right-click on the new package.
  7. Select New -> Servlet .
  8. On Class name, type EJBServlet .
  9. Type the following code in the class:
package org.wikibooks.en;

import java.io.IOException;
import java.io.PrintWriter;

import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EJBServlet extends HttpServlet {
   
    private static final long serialVersionUID = 5847939167723571084L;

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

        out.println("Calling the EJB...");
        try {
            InitialContext initialContext = new InitialContext();

            MyFirstEJB myFirstEJB = (MyFirstEJB) initialContext
                    .lookup("java:global/experience4/experience3/MyFirstEJB");

            out.println(myFirstEJB.sayHello());
        } catch (Exception e) {
            out.println(e);
        }
        out.flush();
        out.close();
    }
}
  1. If you are using another application server than JBoss, you should have to change the lookup java:global/experience4/experience3/MyFirstEJB .
  2. Open the file web.xml in WebContent/WEB-INF .
  3. Before the first markup <servlet>, type the following code:
    <servlet>
        <servlet-name>servlet</servlet-name>
       <servlet-class>org.wikibooks.en.EJBServlet</servlet-class>
    </servlet>
  1. Before the first markup <servlet-mapping>, type the following code:
    <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>/servlet</url-pattern>
    </servlet-mapping>
  1. Right-click Export -> EAR file.
  2. For the destination, choose the deployment folder of the application server.
  3. Click on Finish.
  4. Start your application server.
  5. Go on the URL http://localhost:8080/helloworld/servlet .

You should see Calling the EJB... . It means that you manage to call the servlet. You should also see Hello World! . If you see a text that is a Java exception, it means that the servlet failed to communicate with the EJB. You can verify that the text comes from the EJB by changing the text in the code and redeploy the EAR.

Clipboard

To do:
Add explanations.