Get the online training

Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com

Web Service

Ejb 2.1 is of three types
  • Session Bean
  • Entity Bean
  • MDB

Session bean
  • It maintains business logic of the project.
  • It maintains the conversation between the client and the server.
  • It is not persistent in nature. Data is not saved  to the database.
  • It is removed from the server when the ejb container (server) gets shutdown or crashed.
  • It can be used in transaction. Its transaction aware.
  • It is also  security aware.

    • It is of two types.
      • Stateless bean
      • Stateful Bean
Stateless Bean
  • It is related to conversation  of   only one client.
  • It does not maintain state conversation of  any client.
  • The state can be shared by multiple clients.
  • Ex Credit Card validation, Result of  Exam

Stateful Bean
  • This is related to multiple client.
  • Multiple clients can maintain their separate state.
  • It maintains the state of  conversation.

Stateful ejb2.1 example on Weblogic11g

  • Take a new Ejb  project in  eclipse.
  • Add the name of the project as calculator.
  • Select the vesion of teh ejb Module as 2.1.
  • Click finish.
  • It will create one ejb project.

Stateful Session Bean

  • Create one interface  Demo.java.
  • Save it inside ejbModule in package pack1.
  • Extends EjbObject
  • This is remote interface.
  • In this all the business methods has to be added.
  • We will add one method add.
  • This is also called component interface as it contains all business logic.

package pack1;

import javax.ejb.*;
import java.rmi.*;

created by
shankar jaiswal


public interface Demo extends EJBObject {
  public int add() throws RemoteException;

  • Create one  DemoHome interface.
  • This is home interface.
  • Add the bean life cycle method.
  • It is used for creation and deletion  of bean instance .
  • It maintains all life cycle  bean methods.
  • Here we will one create method.

Stateful bean create method accepts arguments, but stateless bean create method  does not accept any arguments.  

package pack1;

import javax.ejb.*;

created by
shankar jaiswal


public interface DemoHome extends EJBHome {
  public Demo create(int a, int bthrows CreateException,

  • Create  the DemoBean.java
  • It is session bean file.
  • It implements SessionBean interface.
  • We will override methods of  Home interface and Remote interface.
  • Also override methods of SessionBean interface.

package pack1;

import javax.ejb.*;


created by
shankar jaiswal


public class DemoBean implements SessionBean {
  int a, b;

  public void setSessionContext(SessionContext ctx) {

  public void ejbCreate(int a, int b) {
    this.a = a;
    this.b = b;
    System.out.println("EJB CREATED");

  public void ejbActivate() {

  public void ejbPassivate() {

  public void ejbRemove() {

  public int add() {
    return (a + b);

    • Now the structure of the project should be like this.

  • Open ejb-jar.xml

  • This is deployment descriptor file.
  • Here we will add all ejb description and its nature i.e(stateless or stateful)

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1"
  xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">


  • This is vendor specific file.
  • Here we will add  the  ejb name.
  • This should be same as of ejb-jar.xml and weblogic-ejb-jar.xml.
  • Add the jndi name.
  • This will be used by the client to look up.
  • It must be unique.

<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC 
'-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN' 

Deploying ejb application on the server.

  • Deploy the ejb application on the server.

Web Client

  • Ejb can have any  type of client
    • web client
    • standalone client
  • It can be servlet/jsp, struts jsf application or any simple java application.
  • Create one Dynamic Web Project.
  • Add the name of the project as  webClient.

Adding the classpath of ejb project.
  • This project requires the ejb class files which is created earlier.
  • Add the classpath usning build path.
  • Rt Click on the webClient project.
  • Select Build Path-->Configure Build Path.

  • Then Add the calculator ejb project.
  • Click on the Add button.
  • Add the calculator ejb project.

Adding the ejb  jar file in  classpath

  • The application server/web server   requires all ejb class files in the web container.
  • This can be satisfied by adding the ejb in the form of jar file.

  • Add this jar file in webClient\WebContent\WEB-INF\lib.

  • Create one jsp file.
  • save it as client.jsp
  • Add the following code.
  • Here will look up the jndi name which is given in ejb-jar.xml.
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="java.util.Properties"%>
<%@page import="pack1.Demo"%>
<%@page import="pack1.DemoHome"%>


created by
shankar jaiswal


int a=10;
int b=20;

  try {
InitialContext ctx = new InitialContext();
    DemoHome home = (DemoHomectx.lookup("DemoHome2");
    Demo object = home.create(100200);
    out.println("sum of "+a+" and "+b+" is "+object.add());
  catch (Exception e) {
    out.println("Exception caught : " + e);

  • Run this file on the server.
  • It will deploy the web application on the server.
  • Then it will run the jsp file.

Another client file.

<%page import="javax.naming.*"%>
<%page import="java.util.Properties"%>
<%@page import="pack1.DemoHome"%>
<%@page import="pack1.Demo"%>

created by
shankar jaiswal


<%!DemoHome home = null;
  Demo object = null;
  int sum = 0;

  public void jspInit() {
    try {
     InitialContext ctx = new InitialContext();
      home = (DemoHomectx.lookup("DemoHome2");
    catch (Exception e) {
      sum = 0;

  int a = 0;
  int b = 0;

  try {
    a = Integer.parseInt(request.getParameter("n1"));
    b = Integer.parseInt(request.getParameter("n2"));
    if (a != && b != 0){
      object = home.create(a, b);
    sum = object.add();
  catch (Exception e) {
    sum = 0;

<form action="form.jsp">Number1<input name="n1" value="0">
Number2<input name="n2" value="0"> <br>
<input type="submit" value="sum"> <br>

  if (sum != 0)
    out.print("sum of " + a + " and " + b + "  is " + sum);

StandAlone client

We can create stand alone client.

  • Unlike  webClient it is not running on the server.
  • So it requires some entvironment values to be set.
  • It also requires some jar files to be set in its classpath.

Add ejbProject in the build
  • Then Add the calculator ejb project.
  • Click on the Add button.
  • Add the calculator ejb project.

Add wlclient.jar file in the build path.

  • Rt. Click on the project.
  • Select  Build path--->Configure Build Path.
  • Click on the Add external jars.

  • Select wlcliet.jar from C:\Oracle\Middleware\wlserver_10.3\server\lib.

  • Create one Client.java file.
  • Add the following code here.
import java.rmi.RemoteException;
import java.util.Properties;
import java.util.Scanner;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import pack1.Demo;
import pack1.DemoHome;

public class Client {
  public static void main(String[] argsthrows RemoteException,
      CreateException {
    int a, b, c;
    Scanner sc = new Scanner(System.in);
    System.out.println("enter no1 ");
    a = sc.nextInt();
    System.out.println("enter no2 ");
    b = sc.nextInt();

    try {
      Properties p = new Properties();
      p.put(Context.PROVIDER_URL, "t3://localhost:7001");
      InitialContext ctx = new InitialContext(p);
      DemoHome home = (DemoHomectx.lookup("DemoHome2");
      Demo demo = home.create(a, b);
      c = demo.add();
      System.out.println("sum of " + a + " and " + b + " is " + c);
    catch (NamingException e) {



  • Here we have added environment values in the properties.
  • Properties are java collection.
  • It is sub classes of map.
  • Here we have added the values in the form of key and value.

Then pass the property in the InitialContext object.
These environment values are used by the client code to make connection with the server.
If   these values are not given in the program  then it should be given in the .properties file.
Create one jndi.properties file.
Then add the following code to it.
Save it inside ejbModule.


Run the Program

enter no1
enter no2
sum of 55 and 88 is 143

Gui Client

  • We can also create gui client.
  • We will use swing awt api here.
  • Create one AppFrame.java.
  • In the src folder create one jndi.properties file.
  • Add the following code in the jndi.properties file.

  • Open the AppFrame.java
  • Add the following code here.
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.naming.InitialContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import pack1.Demo;
import pack1.DemoHome;

public class AppFrame extends JFrame implements ActionListener {
  JButton show;
  JTextField no1;
  JTextField no2;
  JTextField result;

  public AppFrame() {
    getContentPane().setLayout(new GridLayout(32));
    JLabel n1 = new JLabel("Number1");
    no1 = new JTextField(11);
    JLabel n2 = new JLabel("Number2");
    no2 = new JTextField(11);
    show = new JButton("Addition");
    JLabel result1 = new JLabel("Result");
    result = new JTextField(11);



  public void actionPerformed(ActionEvent evt) {

    int n1 = Integer.parseInt(no1.getText());
    int n2 = Integer.parseInt(no2.getText());

    InitialContext ctx = null;
    try {
      ctx = new InitialContext();
      DemoHome home = (DemoHomectx.lookup("DemoHome2");
      Demo demo = home.create(n1, n2);
      int n3 = demo.add();
      result.setText(new Integer(n3).toString());

    catch (Exception e) {


  public static void main(String args[]) {
    new AppFrame();

The following structure is created.

Run the file.

Download Ejb2.1 eclipse code
Download webClient eclipse code