Tuesday, October 14, 2014

Web Service



Web Service
Server side
Calculator.java
package web;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

@WebService(serviceName = "Calculator")
@Stateless()
public class Calculator {

     /**
     * REMOVE THE DEFAULT OPERATION, DRAG AND DROP NEWLY CREATED OPERATIONS
     */
    @WebMethod(operationName = "add")
    public int add(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
        //TODO write your implementation code here:
        return a+b;
    }

   
    @WebMethod(operationName = "multi")
    public int multi(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
        //TODO write your implementation code here:
        return a*b;
    }
}


Client Side

Crate a console client application.

Right click > other > web service client > add the project
(Before that deploy the web service app)

Pass the values in sever side main method

package calculatorclient;

public class CalculatorClient {

   
    public static void main(String[] args) {
        int x = add(10, 5);
        System.out.println(x);
    }

    private static int add(int a, int b) {
        web.Calculator_Service service = new web.Calculator_Service();
        web.Calculator port = service.getCalculatorPort();
        return port.add(a, b);
    }
   
}

//if you use c# application
//Add the wsdl url to sevice reference
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CalculatorWS.CalculatorClient calc = new CalculatorWS.CalculatorClient();
            int x = calc.add(1, 2);
            MessageBox.Show(Convert.ToString(x));
        }
    }
}

WebApplication1
//Server side same
@WebService(serviceName = "Converter")
@Stateless()
public class Converter {

    double celcious = 0.0;
    double faran =0.0;
   


    @WebMethod(operationName = "CelciouseToFaranheight")
    public double CelciouseToFaranheight(@WebParam(name = "value") double value) {
        //TODO write your implementation code here:
        faran =celcious * 1.8 + 32.0;
        return faran;
    }

  
}

//Client side
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{               
double celcious =Integer.parseInt(jTextField1.getText());
Converter c =new Converter();
double faran =c.CelciouseToFaranheight(celcious);
jTextField2.setText(String.valueOf(faran));
}

No comments:

Post a Comment