This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Invoking a webservice from a servlet" Watch "Invoking a webservice from a servlet" New topic

Invoking a webservice from a servlet

Prashanth Chandra
Ranch Hand

Joined: Dec 07, 2005
Posts: 79
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)
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 );
name = args[ 0 ];
age = new Integer( args[ 1 ] );
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new );
call.setOperationName( new QName("", "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)

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

Joined: Mar 22, 2000
Posts: 13037
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.

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:
subject: Invoking a webservice from a servlet
jQuery in Action, 3rd edition