aspose file tools*
The moose likes Web Services and the fly likes Invoking a webservice from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Invoking a webservice from a servlet" Watch "Invoking a webservice from a servlet" New topic
Author

Invoking a webservice from a servlet

Prashanth Chandra
Ranch Hand

Joined: Dec 07, 2005
Posts: 79
Hi,
I am able to invoke a webservice using a Standalone java program. I am using the below command to invoke the web service.

java com.javasrc.webservices.age.AgeServiceClient -lhttp://localhost:8080/axis/services/AgeService "Siva" 33

Now if I write the whole code in a method I am not able to retreive the values into the servlet. Can any please suggest me how to invoke a web service from a servlet/JSP. Please tell if there any sites for this.

Here is my Client code:

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
public class AgeServiceClient
{
public static void main(String [] args)
{
try
{
Options options = new Options(args);
String endpointURL = options.getURL();
String name;
Integer age;
args = options.getRemainingArgs();
if ((args == null) || (args.length < 2))
{
name = "NoName";
age = new Integer( 0 );
}
else
{
name = args[ 0 ];
age = new Integer( args[ 1 ] );
}
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
call.setOperationName( new QName("http://age.webservices.javasrc.com", "age") );
call.addParameter( "arg1", XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter( "arg2", XMLType.XSD_INT, ParameterMode.IN);
call.setReturnType( org.apache.axis.encoding.XMLType.XSD_STRING);
String ret = (String) call.invoke( new Object[]{name,age});
System.out.println("Age result : " + ret);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}

Thanks in advance
Prashanth
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
Now if I write the whole code in a method I am not able to retreive the values into the servlet.


What happens when you try?

There is nothing magical about invoking a webservice from the servlet environment - you do have to provide for various failure possibilities and Thread safety.

Bill


Java Resources at www.wbrogden.com
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
The same code should be running from a servlet too..What exactly are you getting when you try this.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoking a webservice from a servlet
 
Similar Threads
Use Web Service
how to invoke web service implemented in MS.NET
My first webservice call
Unable to receive result from Web Service Method
Simple Web Service using Axis