Tuesday, January 4, 2011

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:


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 @WebService annotation. To Customize the parameter names in the request use @WebParam annotation

Example: This example exposes the webservices to calculate the area of suare and rectangle.

package com.example.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class Area {

@WebMethod
public double square(@WebParam(name="side") double side)
{
return side * side;
}

@WebMethod
public double rectangle(@WebParam(name="length") double length,@WebParam(name="breadth") double breadth)
{
return length * breadth;
}

public static void main(String[] args) {
Area area = new Area();
String url = "http://localhost:8090/area"; // end point of webservice.
System.out.println(url+"?wsdl");
Endpoint.publish(url, area); // publishing the webservice
}
}

2) Run the below command from source location
apt -d . com\example\ws\Area.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.example.ws.Area

you can view the wsdl of the service using url http://localhost:8090/area?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


<html>
<head>
<script language="javascript">
function call()
{
var side = sideid.value;
var req = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://ws.example.com/\"><soapenv:Body><web:square><side>" + side+ "</side></web:square></soapenv:Body></soapenv:Envelope>";
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var reqXML = xmlDoc.loadXML(req);
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseXML;
alert(response.selectSingleNode(".//return").text);
}
}
var soapaction = "http://ws.example.com/square";
xmlhttp.open("POST","http://localhost:8090/area?wsdl",true);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", soapaction);
xmlhttp.send(req);
}
</script>
</head>
<body>
Side Length: <input type="text" id="sideid"></input>
<button onclick="call();">area of square</button>
</body>
</html>



Donot forget to give your comments /suggestions and refer to your friends.

18 comments:

  1. Excellent post. Thanks bro.

    ReplyDelete
  2. Hi There,

    Looks useful, but I could not make it work.

    Firstly I get "500 Internal Server Error", its not going to request.readyState == 4

    Do u have debugging steps?

    Secondly, For ex: the following 2 lines are not being used
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    var reqXML = xmlDoc.loadXML(req);

    Good Try and Thanks for this !
    -Suba

    ReplyDelete
  3. you can set the statement debugger; in the javascript to see if the request is being fired.

    you can also have sysout statement in java code to get first clue on if the request is actually fired.
    just check in your browser : http://localhost:8090/area?wsdl to see if wsdl is published.

    ReplyDelete
  4. var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    var reqXML = xmlDoc.loadXML(req); where we are using reqXML.We are directly sending request.

    ReplyDelete
  5. I really like learning new things. This is new thing to invoke java webservice from browser using java script. In this post they mentioned two simple steps for invoking java webservice. You can try this concept as its new.

    ReplyDelete
  6. This is first time i read your article.It is helpful.It is good information.I really appreciate you did very great work.Thanks for sharing a wonderful information.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. RABIT a powerful CI framework for an IT Enterprise - http://rabit-update.blogspot.in/
    http://rabitupdates.wordpress.com

    For Details Vist : http://www.lemtom.com

    ReplyDelete
  9. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.

    Java training in Chennai

    Java training in Bangalore

    ReplyDelete

  10. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training
    Selenium training in bangalore

    ReplyDelete
  11. I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
    I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!


    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  12. I‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.

    python training in rajajinagar
    Python training in bangalore
    Python training in usa

    ReplyDelete
  13. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…

    Upgrade your career Learn Data Warehousing Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.

    ReplyDelete
  14. Your topic is very nice and helpful to us … Thank you for the information you wrote.

    Learn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Bangalore Training Academy provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
    Big Data Analytics Training in Bangalore
    Tableau Training in Bangalore
    Data Science Training in Bangalore
    Workday Training in Bangalore

    ReplyDelete

subversion video