• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

consuming .net web service in jsp/servlets

I have a webservice up and running on a remote server.

How do i consume the webservice using JSP or servlets.

note:just for your information, the webservice is implemented on .net platform
0
maXXXeE
Asked:
maXXXeE
  • 4
  • 3
  • 2
  • +1
1 Solution
 
bloodredsunCommented:
>>note:just for your information, the webservice is implemented on .net platform

should make no difference for a webservice, that's one of reasons ws were created :-)

This link should show you how http://radio.weblogs.com/0132036/2003/12/11.html. It basically involves creating a client stub as a helper class and then calling that from the JSP.
0
 
bloodredsunCommented:
In theory you could make the helper class a javabean and use it in a normal manner. As long as it's aware of the web service, it's just another class and you can use it as you would normally.
0
 
KuldeepchaturvediCommented:
Here is a sample code to make a web service call

 WebServiceFactory fact = new WebServiceFactory();
             fact.initWebService(MailImportEndPoint);
             Service filePutService = fact.createWebService("portname", "operation", "portype");
             Call filePutCall = fact.createCall(Service, "operation");
             //Add serializer/deserializer for complex types
             QName qnameAttachment = new QName("http://tempuri.org/mv2003app/type/", "yourownserializer");
             filePutCall.setProperty(org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT, org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);
             ((org.apache.axis.client.Call)filePutCall).registerTypeMapping(
               yourownserializer.class, //Add serializer for attachment.
               qnameAttachment,
               org.apache.axis.encoding.ser.SimpleSerializerFactory.class,
               org.apache.axis.encoding.ser.SimpleDeserializerFactory.class);

filePutCall.invoke(new java.lang.Object[] {attachments[index],"string",string,"String"});

here is the code for facotry
public class WebServiceFactory
{
      private static Object transactionLock = new Object();  
      public static String factoryFileCache = null;
      private final static String DEFAULT_FILENAME = "whatever"; //$NON-NLS-1$
      private final static String DEFAULT_FILENAME_ALT = "whatever";//$NON-NLS-1$
      
      public static Map htService = null;
      public static Map wsdlCache = null;
      
      
      private String wsdlLocation = null;
      private Definition wsdlDef = null;
      private String system = null;
      private String serviceFunction = null;

      // The operation and portType uniquely identify the our Web Service
      private String operation = null;
      private QName operationQName;
      private QName portTypeQName;
      private String ns;
      // The following fields are derived from the WSDL
      private QName portQName;
      private QName serviceQName;
      private Message inputMessage;
      private Message outputMessage;
      private String targetEndpoint = null;

      /**
       * WebServiceFactory constructor.
       */
      public WebServiceFactory()
      {
            super();
      }

      /**
       * Create an RPC Call instance.  The service and port must
       * have already been set.
       * @param service The RPC Service instance.
       * @param operation The operation to perform.
       * @return A RPC Call instance.
       * @throws ServiceException
       */
      public Call createCall(javax.xml.rpc.Service service,
                             String operation) throws ServiceException
      {
            Call call = service.createCall(portQName, operation);
            operationQName = call.getOperationName();
            targetEndpoint = call.getTargetEndpointAddress();
            return call;
      }

      /**
       * Create an RPC Call instance.  The service and port must
       * have already been set.
       * @param service The RPC Service instance.
       * @param operation The QName for the operation to perform.
       * @return A RPC Call instance.
       * @throws ServiceException
       */
      public Call createCall(javax.xml.rpc.Service service,
                             QName operation) throws ServiceException
      {
            Call call = service.createCall(this.portQName, operation);
            operationQName = call.getOperationName();
            targetEndpoint = call.getTargetEndpointAddress();
            return call;
      }

      /**
       * Create an RPC Service instance.  The #initWebService() method
       * must have already been called to read the WSDL definitions.
       * @param operation The operation to perform.
       * @param portType The optional portTpe string.  This will be qualified
       *        with the namespace defined in the WSDL.
       * @return A RPC Service instance.
       */
      public javax.xml.rpc.Service createWebService(String operation, String portType)
      {
            return createWebService((String)null, portType);
      }

      /**
       * Create an RPC Service instance.  The #initWebService() method
       * must have already been called to read the WSDL definitions.
       * @param portName Optional port name.
       * @param operation The operation to perform.
       * @param portType The optional portTpe string.  This will be qualified
       *        with the namespace defined in the WSDL.
       * @return A RPC Service instance.
       * @throws Exception
       */
      public javax.xml.rpc.Service createWebService(String portName, String operation, String portType) throws Exception
      {
            QName qnPortType = null;
            if (portType != null)
                  qnPortType = new QName(getNamespace(), portType);
            
            return createWebService(portName, operation, qnPortType);
      }

      /**
       * Create an RPC Service instance.  The #initWebService() method
       * must have already been called to read the WSDL definitions.
       * @param operation The operation to perform.
       * @param portType The optional portTpe QName.
       * @return A RPC Service instance.
       * @throws Exception
       */
      public javax.xml.rpc.Service createWebService(String operation, QName portType) throws Exception
      {
            return createWebService((String)null, operation, portType);
            
      }

      /**
       * Create an RPC Service instance.  The #initWebService() method
       * must have already been called to read the WSDL definitions.
       * @param portName Optional port name.
       * @param operation The operation to perform.
       * @param portType The optional portTpe QName.
       * @return A RPC Service instance.
       * @throws Exception
       */
      public javax.xml.rpc.Service createWebService(String portName, String operation, QName portType) throws Exception
      {
            determinePort(portName, portType, operation);
            ServiceFactory factory = ServiceFactory.newInstance();
            javax.xml.rpc.Service service = factory.createService(new URL(wsdlLocation), this.serviceQName);
            return service;
      }

      /**
       * Interrogate the WSDL definitions to determine the service/port.
       * Given the operation and the optional portType, set #serviceQName,
       * and portQName.
       * @param portName Optional port name.
       * @param portType The optional portTpe QName.
       * @param operation The operation to perform.
       * @throws Exception
       */
      private void determinePort(String portName, QName portType, String operation) throws Exception
      {
            this.serviceQName = null;
            this.portQName = null;
            
            // Determine port type by finding a matching operation.
            Map pts = wsdlDef.getPortTypes();
            Iterator ptsit = pts.keySet().iterator();
            boolean matchFound = false;
            while (ptsit.hasNext() && !matchFound)
            {
                  PortType pt = (PortType)pts.get(ptsit.next());
                  // If a port type was specified then loop again unless this is a match.
                  if (portType != null && !pt.getQName().equals(portType))
                        continue;
                  
                  portTypeQName = pt.getQName();
                  List ops = pt.getOperations();
                  Iterator o = ops.iterator();
                  while (o.hasNext() && !matchFound)
                  {
                        Operation op = (Operation) o.next();
                        if (operation.equals(op.getName()))
                        {
                              // Operation matches, check binding/service/port to insure
                              // this is the correct portType.
                              matchFound = findMatchingBindingFromPortType(portName, pt.getQName(), operation);
                              if (matchFound)
                              {
                                    if (op.getInput() != null)
                                          inputMessage = op.getInput().getMessage();
                                    
                                    if (op.getOutput() != null)
                                          outputMessage = op.getOutput().getMessage();
                              }
                        }
                  }
            }
            
            if (this.portQName == null)
                  throw new Exception("Port QName is not valid.");
            
            // Normal return with the #serviceQName and #portQName properties set.
      }

      /**
       * Interrogate the WSDL definitions PortTypes find a Binding matching
       * the #operation that will be performed and return <code>true</code>
       * if found.
       * @return Return <code>true</code> if Binding/Service/Port match.
       *         Return <code>false</code> if no match.
       * @param portName Optional port name.
       * @param qnPortType A PortType object.
       * @param operation The operation to perform.
       */
      private boolean findMatchingBindingFromPortType(String portName, QName qnPortType, String operation)
      {
            // Find the binding matching the operation
            QName binding = null;
            Map binds = wsdlDef.getBindings();
            Iterator bindsit = binds.keySet().iterator();
            while (bindsit.hasNext() && binding == null)
            {
                  Binding bind = (Binding)binds.get(bindsit.next());
                  if (qnPortType.equals(bind.getPortType().getQName()))
                  {
                        List ops = bind.getBindingOperations();
                        Iterator opsit = ops.iterator();
                        while (opsit.hasNext())
                        {
                              BindingOperation op = (BindingOperation) opsit.next();
                              if (operation.equals(op.getName()))
                              {
                                    // Operation matches, assume correct portType.
                                    if (findMatchingPortFromBinding(portName, bind.getQName()))
                                          return true;
                              }
                        }
                  }
            }
            
            return false;
      }

      /**
       * Interrogate the WSDL Service/Port definitions to see if the #binding matches.
       * Set the properties #serviceQName, and portQName if a match is found and return
       * <code>true</code>.  If no match is found return <code>false</code>.
       * @return Return <code>true</code> if Service/Port match.
       *         Return <code>false</code> if no match.
       * @param portName Optional port name.
       * @param qnBind A Binding QName.
       */
      private boolean findMatchingPortFromBinding(String portName, QName qnBind)
      {
            // Find the port/service matching the binding
            Map services = wsdlDef.getServices();
            Iterator svcsit = services.keySet().iterator();
            while (svcsit.hasNext() && this.serviceQName == null)
            {
                  Service svc = (Service)services.get(svcsit.next());
                  Map ports = svc.getPorts();
                  Iterator p = ports.keySet().iterator();
                  while (p.hasNext())
                  {
                        Port pt = (Port)ports.get(p.next());
                        // If we have an explicit port, then skip any others
                        if (portName != null && !pt.getName().equals(portName))
                              continue;
                        
                        if (pt.getBinding().getQName().equals(qnBind))
                        {
                              // Binding matches, assume correct portType.
                              this.serviceQName = svc.getQName();
                              this.portQName = new QName(ns, pt.getName());
                              return true;
                        }
                  }
            }
            
            return false;
      }

      /**
       * Return the input Message for the current operation.
       * @return A Message instance describing the input parts.
       */
      public Message getInputMessage()
      {
            return this.inputMessage;
      }

      /**
       * Return the namespace defined in the WSDL.
       * @return The WSDL's target namespace URI.
       */
      public String getNamespace()
      {
            return ns;
      }

      /**
       * Return the table of Message definitions in this WSDL.
       * @return A table of Message definitions.
       */
      public Map getMessages()
      {
            if (wsdlDef != null)
                  return wsdlDef.getMessages();
            else
                  return null;
      }

      /**
       * Return the table of namespace definitions in this WSDL.
       * @return A table of namespace definitions.
       */
      public Map getNamespaces()
      {
            if (wsdlDef != null)
                  return wsdlDef.getNamespaces();
            else
                  return null;
      }

      /*
       * @return Return operationQName.
       */
      public QName getOperationQName()
      {
            return operationQName;
      }

      /**
       * Return the output Message for the current operation.
       * @return A Message instance describing the output parts.
       */
      public Message getOutputMessage()
      {
            return outputMessage;
      }

      /*
       * @return Return portQName.
       */
      public QName getPortQName()
      {
            return portQName;
      }

      /*
       * @return Return portTypeQName.
       */
      public QName getPortTypeQName()
      {
            return portTypeQName;
      }

      /*
       * @return Return serviceQName.
       */
      public QName getServiceQName()
      {
            return serviceQName;
      }

      /*
       * @return Return targetEndpoint.
       */
      public String getTargetEndpoint()
      {
            return targetEndpoint;
      }

      /**
       * Return the table of Types definitions in this WSDL.
       * @return A table of Type definitions.
       */
      public List getTypes()
      {
            if (this.wsdlDef != null && this.wsdlDef.getTypes() != null)
                  return this.wsdlDef.getTypes().getExtensibilityElements();
            else
                  return null;
      }

      /**
       * Return the URI location for the WSDL file.
       * @return A URI string representing the WSDL location.
       */
      public String getWSDLLocation()
      {
            return this.wsdlLocation;
      }

      /**
       * Return the operation for the WSDL file.
       * @return A string representing the WSDL operation.
       */
      public String getWSDLOperation()
      {
            return this.operation;
      }

      /**
       * Initialize our internal configuration.  Read the WSDL and
       * store the WSDL definitions.
       * @param wsdlLocation The URI where the WSDL is located.
       * @throws WSDLException
       */
      public void initWebService(String wsdlLocation) throws WSDLException
      {
            this.wsdlLocation = wsdlLocation;
            readWSDLDefinition(wsdlLocation);
      }

      /**
       * Build a WSDL Definition for a WSDL file using the WSDL
       * factory.
       * @param wsdlUrl The URI where the WSDL is located.
       * @throws WSDLException
       */
      private void readWSDLDefinition(String wsdlUrl) throws WSDLException
      {
            if (wsdlCache == null)
                  wsdlCache = new HashMap();

            wsdlDef = (Definition)wsdlCache.get(wsdlUrl);
            if (wsdlDef == null)
            {
                  long commandBeginTime = 0;

                  WSDLFactory factory = WSDLFactory.newInstance();
                  wsdlDef = factory.newWSDLReader().readWSDL(wsdlUrl);
                  
                  wsdlCache.put(wsdlUrl, wsdlDef);
            }
            
            // Initialize values requried from the wsdl.
            ns = wsdlDef.getTargetNamespace();
      }

      /**
       * This method is responsible for resetting class variables
       */
      public final static void reset()
      {
            htService = null;
            wsdlCache = null;
      }

      /**
       * Return the contents of factoryFileCache.
       * @return Factory configuration file override contents.
       */
      public static String retrieveFactoryFileCache()
      {
            return factoryFileCache;
      }

      /**
       * Retrieve the wsdlCache table which contains our WSDL caches.
       * Initialize the table if not set.
       * @return #wsdlCache table.
       */
      public final static synchronized Map retrieveWsdlCache()
      {
            if (wsdlCache == null)
            {
                  wsdlCache = new HashMap();
            }
            
            return wsdlCache;
      }

      /**
       * Store the updated factory configuration file data in factoryFileCache.
       * @param text Override of factory configuration file data.
       */
      public static void storeFactoryFileCache(String text)
      {
            factoryFileCache = text;
      }

}


0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
KuldeepchaturvediCommented:
and I know writing above class is pretty much your own implementation of factory.. but once its done you can use it pretty much anywhere
0
 
suprapto45Commented:
Hi,

If you have the wsdl file from your .NET platform, you can use Axis to automatically create client stub for you as what bloodredsun told you.

Regards
Dave
0
 
suprapto45Commented:
Hi,

The tools used in Axis are wsdl2java and java2wsdl :)

Regards
Dave
0
 
maXXXeEAuthor Commented:
Hi bloodredsun
I will check ur link

Hi suprapto45
I am not using  Axis
0
 
bloodredsunCommented:
>>I am not using  Axis

What are you using, IBM WS Toolkit?
0
 
maXXXeEAuthor Commented:
i am using JAX-RPC
0
 
suprapto45Commented:
Hi,

Please correct me if I am wrong bloodredsun :)
In my opinion, you should consider to use *tools* in helping you such as Axis or IBM WS Toolkit. It will make your life easier.

Regards
Dave
0
 
bloodredsunCommented:
points for Kuldeep
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now