In this EJB Tutorial, we will code our Stateless Session Bean and access it in the Servlet. You will learn Remote Interface, Context Object and JNDI Name for the remote resource.
1. Remote Interface
1 2 3 4 5 6 7 |
package tube.codingexamples.ejb.statelessbean; import javax.ejb.Remote; @Remote public interface HelloBeanRemote { //Sample 01: Declare the Remote Interface Method public String sayHello(); } |
2. Stateless Session Bean
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 |
package tube.codingexamples.ejb.statelessbean; import javax.ejb.LocalBean; import javax.ejb.Stateless; /** * Session Bean implementation class HelloBean */ @Stateless(mappedName = "HelloBean") @LocalBean public class HelloBean implements HelloBeanRemote { /** * Default constructor. */ public HelloBean() { // TODO Auto-generated constructor stub } @Override public String sayHello() { //Sample 02: Return Greeting Message return "EJB Says: Hello Coding-Example Friends!"; } } |
3. Consuming Servlet
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 |
package tube.codingexamples.ejb.statelessbean; import javax.ejb.LocalBean; import javax.ejb.Stateless; /** * Session Bean implementation class HelloBean */ @Stateless(mappedName = "HelloBean") @LocalBean public class HelloBean implements HelloBeanRemote { /** * Default constructor. */ public HelloBean() { // TODO Auto-generated constructor stub } @Override public String sayHello() { //Sample 02: Return Greeting Message return "EJB Says: Hello Coding-Example Friends!"; } } |
Categories: JavaEE-EJB-Tube