EJB3 | Stateless session bean

To build a statelesss session bean in EJB3, you need to create one interafce and on class. Interface is implemented by class and methods declared in the interaface are available to client for remote access. Bean class and interaface should be public. Bean class is annotated with the annotation @Stateless(mappedName="something") and interafce is annotated with @Remote annotation. Both are compiled and packed in jar file.

Coding for Remote Interface
package p1;
import javax.ejb.Remote;
public interface AdderIntf{
    int add(int x,int y);
Coding for Bean class
package p1;
import javax.ejb.Stateless;
public class Adder implements AdderIntf{
    public int add(int x,int y){
        System.out.println("x="+x+" , y="+y);
        int z=x+y;
        return z;
Both are compiled and packaged in jar file. you can you following command to package in jar file.
>jar cvf myejb.jar p1
Here p1 is the package in with two class files are produced by compiler.

Building client for statelesss session bean
Client for EJB may be application program, servlet or jsp. At the time of coding of client program, we need to pass server specific coding to Context object. Following program is using the EJB hosted  on weblogic server. So you can see some weblogic specific coding here.
import javax.naming.*;
import java.util.*;
class Add{
    public static void main(String ar[])throws Exception{
            Properties p=new Properties();
            InitialContext ctx=new InitialContext(p);
            p1.AdderIntf a1=(p1.AdderIntf)ctx.lookup("abc#p1.AdderIntf");
            int x=a1.add(10,20);
Above program will connect to weblogic server and invoke the method add of the EJB having JNDI name abc#p1.AdderIntf and result is received by this program.

Calculator in java swing

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class gui1{
    static double a=0;
    static int op=0;
    public static void main(String ar[]){
        JFrame f1=new JFrame("Calculator");
        final JTextField t1=new JTextField(100);
        JButton b0=new JButton("0");
        JButton b1=new JButton("1");
        JButton b2=new JButton("2");
        JButton b3=new JButton("3");
        JButton b4=new JButton("4");
        JButton b5=new JButton("5");
        JButton b6=new JButton("6");
        JButton b7=new JButton("7");
        JButton b8=new JButton("8");
        JButton b9=new JButton("9");
        JButton dot=new JButton(".");
        JButton add=new JButton("+");
        JButton sub=new JButton("-");
        JButton equal=new JButton("=");
        JButton div=new JButton("%");
        JButton mul=new JButton("x");
        JPanel buttonPanel=new JPanel();
        buttonPanel.setLayout(new GridLayout(4,4,5,5));
        class MyListener1 implements ActionListener{
            public void actionPerformed(ActionEvent e){
                String cmd=e.getActionCommand();
                }else if(cmd.equals("-")){
                }else if(cmd.equals("x")){
                else if(cmd.equals("%")){
                }else if(cmd.equals("=")){
                    double b=Double.parseDouble(t1.getText());
                    double c=0
                        case 1:c=a+b;break;
                        case 2:c=a-b;break;
                        case 3:c=a*b;break;
                        case 4:c=a/b;break;
                        default :c=0;
                else if(cmd.equals("0")){
                }else if(cmd.equals("1")){
                }else if(cmd.equals("2")){
                }else if(cmd.equals("3")){
                }else if(cmd.equals("4")){
                }else if(cmd.equals("5")){
                }else if(cmd.equals("6")){
                }else if(cmd.equals("7")){
                }else if(cmd.equals("8")){
                }else if(cmd.equals("9")){
                }else if(cmd.equals(".")){
        MyListener1 ml=new MyListener1();

Popular Posts