Creating EJB 3.1 Stateless Session Bean using JBoss 6.1 Example

Stateless session beans are stateless in nature, in other words it does not remember its previous invocations. A user will call the bean and the bean will return a result. A stateless bean is not good for maintaining the state of an interactive session such as required to maintain a list of purchases. However, it is useful for one-time calculations

This tutorial demonstrates creating EJB using @Stateless annotation. The mappedName attribute of @Stateless annotation describe a user friendly-name used to invoke bean.

A session bean can also have a local, remote and no-interface client view. The interface used determines the intent and scope of the bean. The no-interface view is new to EJB 3.1. This approach allows the developer to use EJB without having to declare a business interface.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

packageorg.techzoo.stateless;

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

import javax.ejb.LocalBean;

import javax.ejb.Stateless;

import staticjava.lang.Double.parseDouble;

@LocalBean

@Stateless(mappedName="calcBean")

publicclassCalcBean{

publicdoubleadd(Stringno1,Stringno2){

System.out.println("In add()...");

returnparseDouble(no1)+parseDouble(no2);

}

publicdoublesub(Stringno1,Stringno2){

System.out.println("In sub()...");

returnparseDouble(no1)-parseDouble(no2);

}

@PostConstruct

privatevoidinitialize(){

System.out.println("after intanstiation...");

}

@PreDestroy

privatevoiddestroy(){

System.out.println("before destroying...");

}

}

Accessing a Session Bean using Dependency Injection
There are two techniques for gaining access to an EJB:

Dependency Injection (DI)

Java Naming and Directory Service (JNDI).

DI is the easiest when we can use it, but JNDI can be used in places where DI is not supported. We will look at both of these approaches. However, EJBs should not be created using the Java new keyword.
If the EJB is created using this keyword, then it will no longer be an EJB but rather a regular object. It will not be able to take advantage of the support provided by the EJB container.

To use DI, use @EJB annotation. This will inject EJB reference into our application. Following code demonstrate use of @EJB.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

packageorg.techzoo.servlet;

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;

import org.techzoo.stateless.CalcBean;

publicclassCalcServletextendsHttpServlet{

@EJB privateCalcBean calc;

@Override

protectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)

throws ServletException,IOException{

process(req,resp);

}

privatevoidprocess(HttpServletRequest req,HttpServletResponse resp)

throws ServletException,IOException{

Stringno1=req.getParameter("no1");

Stringno2=req.getParameter("no2");

resp.setContentType("text/html");

PrintWriter out=resp.getWriter();

Stringop=req.getParameter("op");

out.println("<h2>Operation : "+op+"</h2>");

doubleresult=0.0d;

if(op.equals("Add")){

result=calc.add(no1,no2);

}

else{

result=calc.sub(no1,no2);

}

System.out.println("Result : "+result);

out.println("<h4>Result : "+result+"</h4>");

out.println("<a href="\&quot;slsb.jsp\&quot;">Go Back</a>");

out.flush();

out.close();

}

@Override

protectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)

throws ServletException,IOException{

process(req,resp);

}

}

Now create a ear file containing Servlet, EJB and deploy it on JBoss’s root directory. Output will be similar to following screen shot.

About the Author

I Love Coding, Analyzing, Designing and doing it the right way. I am obsessed about learn anything new daily and sharing what I have learn each Day. This Blog is journey of my Zeal, going through step by step to the mountain of desire to become prefect.
GoogleWanna Hire me ?