Invoke a java webservice from browser using javascript ( xmlhttp)
Have you felt tired of seeing pages of matter on invoking a webservice from browser without much understanding . Let us try our way.
The tutorial will have two sections :
1. A simple java method exposed as a webservice ( using java embedded webserver. No need of axis, tomcat etc.,)
2. A simple html page which will invoke the webservice ( no need of php , jsp etc., ) with a simple xmlhttp request / reposnse.
Two steps for Creating and Publishing Webservice using jax-ws:
package com.sree.myservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class ArthimaticOperations {
@WebMethod
public int addNumbers(@WebParam(name="first") int first, @WebParam(name="second") int second){
return first + second;
}
@WebMethod
public int mutiplyNumbers(@WebParam(name="first") int first, @WebParam(name="second") int second){
return first * second;
}
public static void main(String[] args) {
ArthimaticOperations aop = new ArthimaticOperations();
String url = "http://localhost:9090/arthimatic"; // end point of webservice.
System.out.println(url+"?wsdl");
Endpoint.publish(url, aop); // publishing the webservice
}
}
2) Run the below command from source location
apt -d . com\sree\myservice\ArthimaticOperations.java
This command will generate the required wrapper classes one correspoding to request and other corresponding to response.
Now your webservice is ready to use. Just run the class com.sree.myservice.ArthimaticOperations
you can view the wsdl of the service using url http://localhost:9090/arthimatic?wsdl
<html>
<head>
<script language="javascript">
function callMyService()
{
var req = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://myservice.sree.com/\"><soapenv:Body><web:addNumbers><first>" + document.getElementById("firstnum").value + "</first><second>" + document.getElementById("secondnum").value + "</second></web:addNumbers></soapenv:Body></soapenv:Envelope>";
if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseXML;
document.getElementById("resultDIV").innerHTML = response.selectSingleNode(".//return").text;
}
}
var soapaction = "http://myservice.sree.com/addNumbers";
xmlhttp.open("POST","http://localhost:9090/arthimatic?wsdl",true);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", soapaction);
xmlhttp.send(req);
}
</script>
</head>
<body>
<div><label>Enter First Number:</label><input type="text" id="firstnum"></input></div>
<div><label>Enter Second Number:</label><input type="text" id="secondnum"></input></div>
<div><button onclick="callMyService()">Add Numbers</button></div>
<div id="resultDIV"></div>
</body>
</html>
Donot forget to give your comments /suggestions and refer to your friends.
The tutorial will have two sections :
1. A simple java method exposed as a webservice ( using java embedded webserver. No need of axis, tomcat etc.,)
2. A simple html page which will invoke the webservice ( no need of php , jsp etc., ) with a simple xmlhttp request / reposnse.
Two steps for Creating and Publishing Webservice using jax-ws:
1) create a java class and add methods which will be exposed as webservices. To make normal java methods as webservices we have to annotate the methods using@WebMethod annotaiion. Java class must be annotated with @WebServiceannotation. To Customize the parameter names in the request use @WebParamannotation
Example: This example exposes the webservices to add the two numbers and multiply the two numbers
package com.sree.myservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class ArthimaticOperations {
@WebMethod
public int addNumbers(@WebParam(name="first") int first, @WebParam(name="second") int second){
return first + second;
}
@WebMethod
public int mutiplyNumbers(@WebParam(name="first") int first, @WebParam(name="second") int second){
return first * second;
}
public static void main(String[] args) {
ArthimaticOperations aop = new ArthimaticOperations();
String url = "http://localhost:9090/arthimatic"; // end point of webservice.
System.out.println(url+"?wsdl");
Endpoint.publish(url, aop); // publishing the webservice
}
}
2) Run the below command from source location
apt -d . com\sree\myservice\ArthimaticOperations.java
This command will generate the required wrapper classes one correspoding to request and other corresponding to response.
Now your webservice is ready to use. Just run the class com.sree.myservice.ArthimaticOperations
you can view the wsdl of the service using url http://localhost:9090/arthimatic?wsdl
Hope you are getting it running.
Calling webservice from browser using XMLHttpRequest
The webserive exposed can be accessed using soap request sending from browser
Calling webservice from browser using XMLHttpRequest
The webserive exposed can be accessed using soap request sending from browser
<html>
<head>
<script language="javascript">
function callMyService()
{
var req = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://myservice.sree.com/\"><soapenv:Body><web:addNumbers><first>" + document.getElementById("firstnum").value + "</first><second>" + document.getElementById("secondnum").value + "</second></web:addNumbers></soapenv:Body></soapenv:Envelope>";
if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseXML;
document.getElementById("resultDIV").innerHTML = response.selectSingleNode(".//return").text;
}
}
var soapaction = "http://myservice.sree.com/addNumbers";
xmlhttp.open("POST","http://localhost:9090/arthimatic?wsdl",true);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", soapaction);
xmlhttp.send(req);
}
</script>
</head>
<body>
<div><label>Enter First Number:</label><input type="text" id="firstnum"></input></div>
<div><label>Enter Second Number:</label><input type="text" id="secondnum"></input></div>
<div><button onclick="callMyService()">Add Numbers</button></div>
<div id="resultDIV"></div>
</body>
</html>
Donot forget to give your comments /suggestions and refer to your friends.