0
votes

The following is an addition program to add two numbers.

my Server-side coding and Client-side coding as follows.
it throws error like

ReferenceError: com is not defined at (compiled_code):24

To work with Java Adapter Http Adapter is mandatory.

Server.js and client.js as follows

package com.mss;
public class Calculator {
public int addTwoIntegers(String first, String second){
    int c=Integer.parseInt(first)+Integer.parseInt(second);
   return Integer.toString(c);
}

}

function addTwoIntegers(){
alert("hi");
var calcInstance = new com.mss.Calculator();   
  return {
    result : calcInstance.addTwoIntegers("1","2")
  };

}

1
Server.js? can u explain what is this server.js - sasi
@Idan Adar not server.js it is serverside java code i.e Calculator.java. And Client.js means client side javascript. - Vinod Kumar Marupu
So edit your question to make sense. - Idan Adar
@Vinod You cannot call java class directly from client side. For that only you need adapter. Whatever you have written in client.js has to be in adapter. - sasi
@sasi do you mean above two piece of code must be in server side only. - Vinod Kumar Marupu

1 Answers

1
votes

To work with Java Adapter Http Adapter is mandatory

The above sentence in false. In MFP 7.0 you have both JavaScript adapters and Java adapters. To use a Java adapter you are not required to use HTTP adapter. That doesn't make sense. They are two different types of adapters.

Read the following tutorials: Server-side development

Have you taken a look at the UsingJavaInAdapter adapter in the Adapters sample? It demonstrates exactly what you're trying to do.


Did you actually create such a com.mss Java class and placed it in the server\java folder of your MFP project?

The question is just missing information.
Read the Java in JavaScript adapters tutorials.


Java class

package com.sample.customcode;

public class Calculator {

    // Add two integers.
    public static int addTwoIntegers(int first, int second){
        return first + second;
    }

    // Subtract two integers.
    public int subtractTwoIntegers(int first, int second){
        return first - second;
    }
}

Adapter implementation

function addTwoIntegers(a,b){
    return {
        result: com.sample.customcode.Calculator.addTwoIntegers(a,b)
    };
}

function subtractTwoIntegers(a,b){
    var calcInstance = new com.sample.customcode.Calculator();  
    return {
        result : calcInstance.subtractTwoIntegers(a,b)
    };
}