Web Services

    I had always been facinated  about the  Web Services, be it a Java or .net. Its  interesting. 

          When ever i spoke about the Web Services in java with ppl whom i know , exposed to the corporate world , working on Web Services, I never got crisp answer.      Even when  i tried to explore about the Java Web Services, I found that it had multiple dimensions unlike .net where in all you need to write  is a simple class with web method and deploy in IIS and in the Web Service Client create the stub of it and start using it.

             In java, even though its one of the option,life is not so easy, as it is tied up with other components.  The JDK1.6 has the very easy way of coding and publishing Web Services,  Web Service can be written and published by using the JWSDP(Java Web Services Developer Pack) or by using the simple annotations. Lets peep into that after some time.

            I recently just tried to invoke the .Net (c#) webservice to the Java Client.  I would  like to post it with this piece of text. 

     i have used  JDK1.5  Eclipse3.2 with Axis+Tomcat4.1, .net(2.0) .

      Here is  .net Web Service:

             using System;
             using System.Web;
             using System.Web.Services;
             using System.Web.Services.Protocols;

             [WebService(Namespace = “http://tempuri.org/“)]
             [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
            

            public class Service : System.Web.Services.WebService
            {
               public Service () {

                      //Uncomment the following line if using designed components
                      //InitializeComponent(); 
             }

                  [WebMethod]
                  public string myWeb(String arg1) {
                   return “Hello “+arg1;
                 }
            }

  This web method accepts a string and prefixes the Hello string and return the string.

         Now we got to code for the Client,  As i said i am using Eclipse 3.2 (Objectweb with lomboz J2ee IDE +tomcat4.1+Axis).  This is the best J2ee ide i have ever seen.  follow the instructions

  •  create the new Web Service Project  File->new->other->Webservices. 
  • A wizard will appear in which  you need to select the kind of web service you are planning to develop , There are 2 types  first on is “bottom up”, where in you are writing a webservice and deploying in the webserver which is not of our interest, Second is  for invoking the Web Service  this type of web service is called “top down” which is of our interest for now.
  • Provide the WSDL Url .
  • Say finish, and you are done

      Now  Eclipse will create the proxy of the WSDL  and there will be around 5 files created as shown in the figure below.

eclipse package Explorer

  Now We shall code for the client.  We need to import the proxy  Package  created as shown in the program below:

  Web Service  Client:

                       package myPackage;
                       import org.tempuri.*;      // Proxy package

                       public class WebClient {
 
                       private Service myService;
                       private ServiceSoap port;
    
                        public WebClient()
                     {
                         //Make a Service
                          myService =new ServiceLocator();
                     }
 
                    public String invokeService(String text) throws Exception{
      
                                           //Now use the service to get a stub
                                               port=myService.getServiceSoap12();  
                                          //Make the actual call
                                              String stg=port.myWeb(text);
                                              return stg;
                                          }
                                       }

         Client GUI: I will omit that code as its not much significant.

The GUI looks as follows:

          swingclient.JPG(just ignore the hapazard layout)

  Now you are done. make sure the IIS is up and running and execute the Client java Program.     Just enter Some text and click on the Invoke button. and  you will invoke the Web Service the C# Web Service will append “Hello” with your text and prints in the status box.

              So that was one way to invoke Web Service in java.   

      With the JDK1.6, Web Service is integrated within, and we use some thing called anotations(@)  Here is a nice article about it.

3 Comments

  1. Hey, that plugin seems to be very promising, But i am not able to gather info about its usage. I tried to download the Sourcecode and tried to compile, due to the lack of documentation i didnt get any info about it. This Plugin is plugged to wat? Do throw some light on it..

    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s