Stateless session beans are stateless in nature, in different phrases it doesn’t keep in mind its earlier invocations. A person will name the bean and the bean will return a consequence. A stateless bean shouldn’t be good for sustaining the state of an interactive session similar to required to keep up a listing of purchases. Nonetheless, it’s helpful for one-time calculations
This tutorial demonstrates creating EJB utilizing @Stateless
annotation. The mappedName
attribute of @Stateless
annotation describe a person friendly-name used to invoke bean.
A session bean may have a neighborhood, distant and no-interface consumer view. The interface used determines the intent and scope of the bean. The no-interface view is new to EJB 3.1. This strategy permits the developer to make use of EJB with out having to declare a enterprise 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
|
package deal org.techzoo.stateless;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import static java.lang.Double.parseDouble;
@LocalBean
@Stateless(mappedName = “calcBean”)
public class CalcBean {
public double add(String no1, String no2){
System.out.println(“In add()…”);
return parseDouble(no1)+parseDouble(no2);
}
public double sub(String no1,String no2){
System.out.println(“In sub()…”);
return parseDouble(no1)–parseDouble(no2);
}
@PostConstruct
non-public void initialize(){
System.out.println(“after intanstiation…”);
}
@PreDestroy
non-public void destroy(){
System.out.println(“earlier than destroying…”);
}
}
|
Accessing a Session Bean utilizing Dependency Injection
There are two strategies for having access to an EJB:
- Dependency Injection (DI)
- Java Naming and Listing Service (JNDI).
DI is the simplest after we can use it, however JNDI can be utilized in locations the place DI shouldn’t be supported. We are going to take a look at each of those approaches. Nonetheless, EJBs shouldn’t be created utilizing the Java new key phrase.
If the EJB is created utilizing this key phrase, then it’ll not be an EJB however slightly a daily object. It won’t be able to reap the benefits of the assist offered by the EJB container.
To make use of DI, use @EJB
annotation. This can inject EJB reference into our software. Following code show 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
|
package deal org.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;
public class CalcServlet extends HttpServlet {
@EJB non-public CalcBean calc;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
course of(req,resp);
}
non-public void course of(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String no1 = req.getParameter(“no1”);
String no2 = req.getParameter(“no2”);
resp.setContentType(“textual content/html”);
PrintWriter out = resp.getWriter();
String op = req.getParameter(“op”);
out.println(“
Operation : “+op+““ );double consequence = 0.0d;
if(op.equals(“Add”)){
consequence = calc.add(no1, no2);
}
else {
consequence = calc.sub(no1, no2);
}
System.out.println(“Outcome : “+consequence);
out.println(“
Outcome : “+consequence+““ );out.flush();
out.shut();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
course of(req, resp);
}
}
|
Now create a ear file containing Servlet, EJB and deploy it on JBoss’s root listing. Output shall be much like following display shot.
Supply techzoo.org