Developing the JavaBean CalcProxy

Use this procedure to create a JavaBean that retrieves the home interface of the Calculator session bean by performing a lookup operation in the naming system, and which invokes the bean’s business methods. The JavaBean also passes the numbers entered in the JSP to the enterprise bean and then returns the result to the JSP.


You have created the Web module project CalculatorWeb, and you have added the EJB module project CalculatorEjb to its build path.


Creating the JavaBean class


       1.      In the J2EE Explorer, select CalculatorWeb and open the context menu.

       2.      Choose New ® Package….

       3.      Enter as the package name. Choose Finish.

The package is created in CalculatorWebt/source directory.

       4.      In the J2EE Explorer, select CalculatorWeb again and choose New ® Java Class...from the context menu.

       5.      To specify the JavaBean, choose Browse next to the Package field and then choose

       6.      Enter CalcProxy in the Name field.

       7.      Leave the other default settings unchanged and choose Finish.

The wizard generates a new java file and opens the Java editor automatically, displaying the generated content of the new class.

Adding Source Code


       1.      Add the following source code to implement the logic of the CalcProxy.

                            a.      First, define a method that retrieves the Calculator bean’s home interface by performing a lookup operation, and initializes the enterprise bean by invoking the create() method of the home interface. You will call this method init().


public class CalcProxy {


   private Calculator calc;


   public void init() throws Exception {


       //Lookup the enterprise bean

       try {

          InitialContext ctx = new InitialContext();

          Object ob = ctx.lookup("java:comp/env/ejb/CalculatorBean");

          CalculatorHome home = ( CalculatorHome ) PortableRemoteObject.narrow( ob, CalculatorHome.class );


         //Initialize the enterprise bean

          calc = home.create();

       } catch ( Exception e ) {

          throw new Exception("Error instantiating Calculator EJB" + e.toString());




                            b.      Next, write the constructor of the class and invoke the init() method in the constructor.


   public CalcProxy() throws Exception {




                            c.      Finally, write a method getResult() that parses the input parameters, invokes the enterprise bean’s business methods, and returns the result of the relevant calculation.


public float getResult( String firstNumber, String secondNumber, String expression ) throws Exception {

   float result = 0;

   try {

      if ( firstNumber != null && secondNumber != null ) {


          //Parse the input parameters

          float first = Float.parseFloat( firstNumber );

          float second = Float.parseFloat( secondNumber );

          int expr = Integer.parseInt( expression );


          //Invoke the relevant method of the enterprise bean

          switch ( expr ) {

              case 1:

                 result = calc.multiply( first, second );


              case 2:

                 result = calc.divide( first, second );


              case 3:

                 result = calc.add( first, second );


              case 4:

                 result = calc.subtract( first, second );




   }catch (Exception re){

       throw new Exception("Fill in all required fields with appropriate values!");



    //Return the result of the calculation

   return result;



       2.      If necessary, correct the formatting of the code lines by choosing Source ® Format from the context menu.

       3.      To add the required import statements, position the cursor anywhere in the Java editor and choose Source ® Organize Imports.

       4.      Choose  javax.rmi.PortableRemoteObject and confirm by choosing Finish.

The appropriate import statements are added to the source code.

       5.      Save the contents of the editor using the appropriate icon from the toolbar.

The JavaBean CalcProxy is also automatically compiled in CalculatorWebt/bin directory. To navigate to this directory, choose the Navigator view.


You have created and completely implemented the JavaBean CalcProxy, which will serve as the controller part of your Calculator application.

Next step:

Next you develop the Calculator JSP.