Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I created a web service call in netbeans using an online tutorial. It works great except that the web service loads when the page is loaded. how can i change it so that the webservice gets called when a link or icon is clicked?

below is the code that it generated

JavaScript
<%
     try {
         webservice.CalculatorService service = new webservice.CalculatorService();
         webservice.Calculator port = service.getCalculatorPort();
         int nodeType = 0;
         java.lang.String result = port.treeList(nodeType);
     } catch (Exception ex) {
         //out.println("Error " + ex);
     }
%>


can i make this into a function where it can be called from a link or button click on the jsp page?
Posted

Hi.
I'm an "old school" developer, so you may take this response as a joke. Simply by changing the flow of the page guided by a request parameter, you can write client HTML code in one flow, and in other flow call the WS by the request parameter argumented in that call. For example, suppose this is your JSP page (test.jsp) :

...
<![CDATA[<%
         if (request.getParameter("yesWeCan") != null){
             try {             
               webservice.CalculatorService service = new webservice.CalculatorService();
               webservice.Calculator port = service.getCalculatorPort();
               int nodeType = 0;
               java.lang.String result = port.treeList(nodeType);             
             } catch (Exception ex) {
                 //out.println("Error " + ex);
             }
         }
	%>

...


and from your Javascript you've goto to call JSP page like this :
http://.../test.jsp?yesWeCan=true

Furthermore, when parameter "yesWeCan" is not null, you don't need to write any HTML code to the client, so you can call this in an asynchronous way using Ajax, for example.

Hope this helps.

Regards.
 
Share this answer
 
v2
Comments
RaviRanjanKr 29-Nov-11 9:39am    
[Edited]Code is wrapped in "pre" tag[/Edited]
solution is simple all you need is jquery $.ajax function and to do so i'd given a small app which i'd used in my project.

all you need to configure is provide url of you page upto it's webmethod and provide datatype it's giving as in my case i'd written json is dataType

and all set you can fetch result by result.d

url1 = "yourpage.aspx/webmethod"
$.ajax({
type: "POST",
url: url1,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (result) {
if (result.d == 'true') {
}
else {

}
}

});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900