Information For Applications of Ws

Download as docx
Download as docx
You are on page 1of 4

Invoking Webservice using Servlet

Jun 19, 2008 1:14 AM

10302589 Reply »

Hi guys
I am working with apache axis . I have successfully created web service in axis. They are
working fine when I am using the simple client. the code for client is below

package samples.userguide.project;

//import java.beans.Statement;
//import java.security.Provider.Service;
import java.sql.*;
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 java.util.Vector;
import javax.xml.rpc.ParameterMode;
//import org.omg.CORBA.ParameterMode;
//import sun.net.dns.ResolverConfiguration.Options;

public class Webclient {

public static void main(String[] args) throws Exception {


Options options = new Options(args);
String endpoint = "http://localhost:" + options.getPort() +"/axis/services/SqlService";
args = options.getRemainingArgs();

if (args == null || args.length != 1) {


System.err.println("Usage: Arguments is not sufficient");
return;
}

String user = new String(args[0]);


Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("retrieveTable");
call.addParameter("op1", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
String ret = (String) call.invoke(new Object[]null);
System.out.println(ret);

}
}
but now I want to implement this client in a servlet. I tried the same code with servlet but it is not
working . the code which I used for the Servlet is below:
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
import java.sql.*;
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 java.util.Vector;
import javax.xml.rpc.ParameterMode;

public class testservlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

//@WebServiceRef(wsdlLocation =
"http://localhost:8080/JaxWsTest/JaxWsServiceService?wsdl")
Service serv;

String endpointURL = "http://localhost:8080/axis/SqlService?wsdl";


String user = "jailly.benjamin";
protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
serv = new Service();
try{
Call call = (Call) serv.createCall();

call.setTargetEndpointAddress( new java.net.URL(endpointURL) );


call.setOperationName("retrieveTable");
call.addParameter("op1", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
String ret = (String) call.invoke(new Object[]null);

response.getWriter().println(ret);
}
catch(Exception e){
}
response.getWriter().close();
}
}
but it is not working what should i do ? and where should i put the jar files which was required to
run the simple client ? When i tried to call it it shows the following err

package com.iton;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import javax.xml.rpc.holders.ObjectHolder;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import com.sun.corba.se.spi.oa.ObjectAdapterFactory;

/**
* Servlet implementation class for Servlet: Invoke
*
*/
public class Invoke extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
static final long serialVersionUID = 1L;

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public Invoke() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest
request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub

String endpointURL =
"http://localhost:8080/SamplewsCal/services/Calculations?wsdl";
Service serv = new Service();
try{

Call call = (Call) serv.createCall();

call.setTargetEndpointAddress( new java.net.URL(endpointURL) );


call.setOperationName("add");
call.addParameter("1", XMLType.XSD_INT, ParameterMode.IN);
call.addParameter("5", XMLType.XSD_INT, ParameterMode.IN);
call.setReturnType(XMLType.XSD_INT);
int ret = (int) call.invoke(new int[]);

response.getWriter().println(ret);
}
catch(Exception e)
{
response.getWriter().close();
}
}

You might also like