Thursday, January 27, 2011

Begning with Simple WebService

WebService Link






Steps for WebService:In Netbeans create a new project as new webapplication

  1. New Web Application set source level to 1.6JDK.
  2. RightClick to project to new->webservice.
  3. Give webservice Name and package.
  4. Open webservice folder right click in the webservice (public class Test {) ->click add operation
  5. define webmethod add and web variables i and j.
  6. Properties -> Run->relative url “/CalculatorWS?Tester” for Tomcat
  7. Deploy webservice.



HelloWebService

           

package org.me.calculator;



import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;



/**

 *

 * @author harshals

 */

@WebService()

public class CalculatorWS {

    /**

     * Web service operation

     */

    @WebMethod

    public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {

        // TODO implement operation

        int k=i+j;

        return k;

    }

   

Steps to create the WebServiceClient.

1.       New Java Client

2.       Right JavaClient -> WebService Client

3.       Project browse to Webservice or provide the wsdl location “ http://localhost:8084/HelloWebservice/CalculatorWS?wsdl

4.       Right click in main ->Webservice Client resources.

5.       Run Project

6.       Voila webservice is ready.



Main.java

package hellojavaclient;



/**

 *

 * @author harshals

 */

public class Main {

   

    /** Creates a new instance of Main */

    public Main() {

    }

   

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        try { // Call Web Service Operation

            org.me.calculator.client.CalculatorWSService service = new org.me.calculator.client.CalculatorWSService();

            org.me.calculator.client.CalculatorWS port = service.getCalculatorWSPort();

            // TODO initialize WS operation arguments here

            int i = 0;

            int j = 0;

            // TODO process result here

            int result = port.add(i, j);

            System.out.println("Result = "+result);

        } catch (Exception ex) {

            // TODO handle custom exceptions here

        }

        // TODO code application logic here

    }

   

}

No comments:

Post a Comment