EJB Hello world example

Creating and testing the "Hello World" Example is the very first step towards learning of any application or programming language.

EJB Hello world example


Creating and testing the "Hello World" Example is the very first step towards learning of any application or programming language. In the given example we are going to show you, how to create your first hello world example in EJB and testing it.

You can also create a hello world example to test your EJB environment setup. This simple application will required three different files to print the message.

  1. SessionBeanRemote.java:-  This  is the Remote interface which extends javax.ejb.EJBObject package. These are similar to RMI Remote interface. The use of remote interface is particularly helpful in  providing business-specific functionality of an EJB.Here @Remote is the annotation used to declare the interface as Remote.

  String getAddress(); String getCompanyname(); String getResult():-These are the methods which is to be defined in the bean.

  2.   SessionBeanBean.java:-This is the bean of type session in which we have defined  the body of the method which were declared in the  
  interface named SessionBeanRemote.java.@Stateless is the annotation used to declare the bean as a session type.

    3.    Main.java:-This is the client application from which  we can access the methods which are defined in the bean.@EJB is the annotation 
  used for configuring the EJB values for a field and  method.


package ejb;

import javax.ejb.Remote;

public interface SessionBeanRemote {

  String getResult();

  String getAddress();

  String getCompanyname();


package ejb;

import javax.ejb.Stateless;

public class SessionBeanBean implements
SessionBeanRemote,SessionBeanLocal {

  public String getResult() {
  return "Hello World";
  public String getAddress() {
  return "Sec-3,D-16/116,Rohini";
  public String getCompanyname() {
  return "Roseindia.net Pvt.Ltd.";


package enterpriseappee5;

import ejb.SessionBeanRemote;
import javax.ejb.EJB;

public class Main {

  private static SessionBeanRemote sessionBeanBean;

  public static void main(String[] args) {
  System.out.println("Displaying Message using EJB:");
  System.err.println("Name of the Company is : =" 
+ sessionBeanBean.getCompanyname());
  System.err.println("Address of the Company is : =" 
+ sessionBeanBean.getAddress());
  System.err.println("Message is : ="
+ sessionBeanBean.getResult());

Output of the program

 Displaying Message using EJB:
Name of the Company is : =Roseindia.net Pvt.Ltd.
Address of the Company is : =Sec-3,D-16/116,Rohini
Message is : =Hello World

Download Source code

Share on Google+Share on Google+

EJB Hello world example

Posted on: January 12, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: EJB Hello world example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 29, 2011

How to run the above(EJB Hello world example) application? I am using Eclipse IDE with WebSphere server. I had created all the three above mentioned files.
October 20, 2011

SessionBeanLocal - does not exist.
November 29, 2011

Hi, I have created the ejb, but how can I deploy it on JBOSS AS?
March 19, 2012

What is SessionBeanLocal here ? Why you have used main function to demostrate EJB ? It is ridiculous to use main for this, someone can ask you "and what is the diference between SE java Bean and EJB?". You have not explained it.
January 8, 2013
Improper demo

Some error lies in the above code. 1)SessionBeanBean implements SessionBeanLocal which has not defined in whole programme. 2)In Main Class we should instantiate the object sessionBeanBean otherwise it gives us null pointer exception: it should be like that, in main function sessionBeanBean=new SessionBeanBean(); then we have to call the methods.