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
}
}
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.
Excellent post. Thanks bro.
ReplyDeleteHi There,
ReplyDeleteLooks 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
you can set the statement debugger; in the javascript to see if the request is being fired.
ReplyDeleteyou 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.
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
ReplyDeletevar reqXML = xmlDoc.loadXML(req); where we are using reqXML.We are directly sending request.
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.
ReplyDeleteThis 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.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanx....good example
ReplyDeleteAnkit
RABIT a powerful CI framework for an IT Enterprise - http://rabit-update.blogspot.in/
ReplyDeletehttp://rabitupdates.wordpress.com
For Details Vist : http://www.lemtom.com
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.
ReplyDeleteJava training in Chennai
Java training in Bangalore
ReplyDeleteIt 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
I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
ReplyDeleteI 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
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.
ReplyDeletepython training in rajajinagar
Python training in bangalore
Python training in usa
Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…
ReplyDeleteUpgrade your career Learn Data Warehousing Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.
Your topic is very nice and helpful to us … Thank you for the information you wrote.
ReplyDeleteLearn 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
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like th
ReplyDeleteDevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery
is.
Awaited information.Thank you for sharing the grat information with us.Java training in Chennai
ReplyDeleteJava training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
Thankyou for such a nice content. Very Informative.
ReplyDeletedata science training in chennai
ccna training in chennai
iot training in chennai
cyber security training in chennai
ethical hacking training in chennai