Jakarta EE Programming/Stateless Session Beans
Appearance
Here is a short tutorial to use a stateless session EJB using Eclipse.
- In Eclipse, right-click on the Project Explorer view.
- Select New -> EJB Project. If EJB Project doesn't appear, Select New -> Other, select EJB -> EJB Project and click on Next.
- On "Project name", type
helloworld-ejb
. - Click on Finish.
- Right-click on the Project Explorer view.
- 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.
- On "Java package", type
org.wikibooks.en
. - On "Class name", type
MyFirstEJB
. Leave the other options as it is. - Click on Finish.
- On the same package, create a new interface
MyFirstEJBRemote
. - Add the following method signature inside it:
public String sayHello();
- Add the following annotation above the signature of the interface:
@Remote
- Open the class
MyFirstEJB
. - Remove the annotation
@LocalBean
. - Add the following method inside it:
public String sayHello() { return "Hello World!"; }
- Right-click on the project.
- 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
. - Choose a location for the destination.
- Click on Finish .
- Go on the folder where you have created your JAR.
You should see a JAR file named helloworld-ejb.jar
. You can delete it.
- Right-click on the Project Explorer view.
- Select New -> Enterprise Application project.
- On "Project name", type
helloworld-ear
. - Click on Next .
- Select the project
helloworld-ejb
. - 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 . - Right-click Export -> EAR file, choose a destination and click on Finish.
- Create a copy of the EAR file and change the extension to .zip .
- Explore the content of the ZIP file.
You should see the JAR file named helloworld-ejb.jar
inside. You can delete the ZIP file.
- Copy/paste your EAR file in the deployment folder of your application server.
- Start your application server.
Now your EJB is usable. Unfortunately, we don't know how to use it yet.
- Shutdown your application server.
- Reuse the WAR project that you created in this page.
- Right-click on Java Resources/src .
- Select New -> Package .
- On name, type
org.wikibooks.en
. - Right-click on the new package.
- Select New -> Servlet .
- On Class name, type
EJBServlet
. - 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(); } }
- If you are using another application server than JBoss, you should have to change the lookup
java:global/experience4/experience3/MyFirstEJB
. - Open the file
web.xml
in WebContent/WEB-INF . - Before the first markup
<servlet>
, type the following code:
<servlet> <servlet-name>servlet</servlet-name> <servlet-class>org.wikibooks.en.EJBServlet</servlet-class> </servlet>
- Before the first markup
<servlet-mapping>
, type the following code:
<servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping>
- Right-click Export -> EAR file.
- For the destination, choose the deployment folder of the application server.
- Click on Finish.
- Start your application server.
- 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.