Joe Moore
Joe Moore

Reputation: 9

Simple Web Service adding two numbers

I have created a simple Webservice function as shown below;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ws;
import javax.jws.WebService;
/**
 *
 * @author Joe
 */
@WebService()
public class Add2Int {
public int add(int a, int b) {
    return (a+b);
}
}

and I have created a very simple gui that allows the user to enter 2 numbers and which should output the result however this does not work? I tried it without the gui and it works but when i build the gui it does not work? here is my code for that side of things

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package myjavawsclient;
//import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Joe
 */
public class Calculator extends JFrame implements FocusListener {
    JTextField value1 = new JTextField("", 5);
    JLabel plus = new JLabel("+");
    JTextField value2 = new JTextField("",5);
    JLabel equals = new JLabel("=");
    JTextField sum = new JTextField("", 5);

    public Calculator() {
        super("The Calculator");
        setSize(350,90);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
        setLayout(flow);
        // add the listners
        value1.addFocusListener(this);
        value2.addFocusListener(this);
        // set up sum field
        sum.setEditable(true);
        //add componets
        add(value1);
        add(plus);
        add(value2);
        add(equals);
        add(sum);
        setVisible(true);
    }

    public void focusGained(FocusEvent event){
        try { // Call Web Service Operation
            ws.Add2IntService service = new ws.Add2IntService();
            ws.Add2Int port = service.getAdd2IntPort();
            // TODO initialize WS operation arguments here
            int result = 0;
            int result2 = 0;
            result = Integer.parseInt(value1.getText());
            result2 = Integer.parseInt(value2.getText());

            int total = port.add(result, result2);
            sum.setText("" +total);

            //float plusTotal = Float.parseFloat(value1.getText()) +
                Float.parseFloat(value2.getText());

        } catch (Exception ex) {
            // TODO handle custom exceptions here
            //value1.setText("0");
            //value2.setText("0");
            //sum.setText("0");
        }
    }

    public void focusLost(FocusEvent event){
      focusGained(event);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // TODO code application logic here

        Calculator frame = new Calculator();
    }
}

I am not getting any errors I am just not getting any result from the 2 numbers, for example 1+1=2 but with my application it allows the user to enter 1 + 1 = ? but where the question mark is nothing gets shown.

I was wondering if anyone could solve this problem for me. Oh and I am using NetBeans and GlassFish App server with WSDL

Joe

Upvotes: 0

Views: 11478

Answers (2)

Joe Moore
Joe Moore

Reputation: 9

My Fault! I forgot to start the App Server

Upvotes: 0

Alexander Sobolev
Alexander Sobolev

Reputation: 789

You should declare add as a webmethod. try following:

   @WebMethod public int add(int a, int b){
    return (a+b);
    }

Upvotes: 0

Related Questions