This tip demonstrates how to create a web application in Java. Let us start with creating a webservice with the name web_service_demo.
After creating the application , we need to create a package. For creating package right-click on the application project name ,than click on package link. It will show you a pop-up box where you can put your package name , but here i am using the package name as “mypackage”.
Now we are all set to create the web service.So for creating the web service ,right click on the project name , then in side the “New” link it will show the another panel like this…
Than click on the “Web Service” .It will show a box like this…
After putting the details ,click on “finish”. It will create a class file with name “ArithmeticOperation.java” inside the mypackage. The initail code inside the class file is like this /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mypackage; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * * @author hareramr */ @WebService(serviceName = “ArithmeticOperation”) public class ArithmeticOperation { /** This is a sample web service operation */ @WebMethod(operationName = “hello”) public String hello(@WebParam(name = “name”) String txt) { return “Hello ” + txt + ” !”; } } Netbeans IDE has created a sample web service operation.But for creating yor own web method,right click inside the class file. It will open a popup like this…
Now click on the “Insert Code..”. It will show you another box where you can select the “Add Web Service Operation…”. After clicking on it,it will show you box like this …
Here you can add web method name, return type of method,parameters with data types and others.I have already added the information in the above image file.This web method will do the basic mathematical operation on the parameter1 and parameter2 based on the operator you have given. After adding the code the class file will look like this … /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mypackage; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * * @author hareramr */ @WebService(serviceName = “ArithmeticOperation”) public class ArithmeticOperation { /** * Web service operation * it will do calculation on parameter1 and parameter2 with the operator */ @WebMethod(operationName = “operation”) public String operation(@WebParam(name = “parameter1”) double parameter1, @WebParam(name = “parameter2”) double parameter2, @WebParam(name = “operator”) String operator) { double result = 0.0; String message = “Your result is :”; try { switch (operator) { case “+”: result = parameter1 + parameter2; break; case “-“: result = parameter1 – parameter2; break; case “*”: result = parameter1 * parameter2; break; case “/”: result = parameter1 / parameter2; break; default: message = “Sorry! Invalid operator , try another”; break; } } catch (ArithmeticException e) { message = e.getMessage(); } catch (Exception e) { message = e.getMessage(); } return message + result; } } Now the web serive has been created. So to test it, right click on the ArithematicOperation under Web Servics.The below image will show the clicking link.
After clicking on the “Test Web Service” it will show you a page like this …
Now you can put your values to test it.
So here the first output for values “29”,”25”,”-”.