Jakarta EE Programming/Stateless Session Beans

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

public String sayHello; @Remote public String sayHello { return "Hello World!"; }
 * 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.
 * 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.
 * 8) On "Class name", type   . Leave the other options as it is.
 * 9) Click on Finish.
 * 10) On the same package, create a new interface.
 * 11) Add the following method signature inside it:
 * 1) Add the following annotation above the signature of the interface:
 * 1) Open the class.
 * 2) Remove the annotation.
 * 3) Add the following method inside it:
 * 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.
 * 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. You can delete it.


 * 1) Right-click on the Project Explorer view.
 * 2) Select New -> Enterprise Application project.
 * 3) On "Project name", type.
 * 4) Click on Next.
 * 5) Select the project.
 * 6) Click on Finish . You should have a new project called  . 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  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.

package org.wikibooks.en;
 * 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.
 * 6) Right-click on the new package.
 * 7) Select New -> Servlet.
 * 8) On Class name, type.
 * 9) Type the following code in the class:

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; } }       servlet org.wikibooks.en.EJBServlet  servlet /servlet 
 * 1) If you are using another application server than JBoss, you should have to change the lookup.
 * 2) Open the file   in WebContent/WEB-INF.
 * 3) Before the first markup , type the following code:
 * 1) Before the first markup , type the following code:
 * 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.

You should see. It means that you manage to call the servlet. You should also see. 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.