How to Use axis to develop Web service instances

Source: Internet
Author: User

1. axis installation 1. j2se SDK 1.3 or 1.4: I use 1.4.2 servlet container: Tomcat 5.0

2. Download The axisinstallation package at http://ws.apache.org/axis/

3. decompress the installation package and copy the axis package under axis_unzip_path/axis-version/webapps to tomcat_home/webapps/. The axis_home directory is the tomcat_home/webapps/axis directory.

4. start Tomcat and access http: // localhost: 8080/axis to check whether the installation is successful.
5. The preceding steps are successfully executed. You can develop a WebService example.

Axis supports the deployment and development of three web services:

1. dynamic invocation interface (DiI)

2. stubs Mode

3. Dynamic proxy

Ii. Compile a dynamic invocation interface Web Service

1. Write the server program helloclient


      public class HelloClient{    public String getName(String name)    {        return "hello "+name;    }}

2. Copy the source code to axis_home and rename it helloclient. JWS.

3. Access http: // localhost: 8080/axis/helloclient. JWS? WSDL: The page displays the WSDL automatically generated by axis.

4. Write the client testhelloclient. Java to access the service.


      import org.apache.Axis.client.Call;import org.apache.Axis.client.Service;import javax.xml.namespace.QName;import javax.xml.rpc.ServiceException;import java.net.MalformedURLException;import java.rmi.RemoteException;public class SayHelloClient2{    public static void main(String[] args){        try{  String endpoint =  "http://localhost:8080/Axis/HelloClient.jws";    Service service = new Service();            Call call = null;            call = (Call) service.createCall();            call.setOperationName(new QName(               "http://localhost:8080/Axis/HelloClient.jws",    "getName"));            call.setTargetEndpointAddress(new java.net.URL(endpoint));            String ret = (String) call.invoke(new Object[] {"zhangsan"});            System.out.println("return value is " + ret);        } catch (Exception ex){       ex.printStackTrace();        }    }}

3. Write dynamic proxy to access the service

1. Write and deploy the server program in the same way as the DiI method on the same side. The helloclient deployed above is still used this time.

2. Compile the proxy Interface


      public interface HelloClientInterfaceextends java.rmi.Remote{    public String getName(String name)throws java.rmi.RemoteException;}

3. Write and execute the client program testhelloclient. Java


      import javax.xml.rpc.Service;import javax.xml.rpc.ServiceFactory;import java.net.URL;import javax.xml.namespace.QName;public class TestHelloClient {    public static void main(String[] args){        try        {            String wsdlUrl = "http://localhost:8080/Axis/HelloClient.jws?wsdl";            String nameSpaceUri = "http://localhost:8080/Axis/HelloClient.jws";            String serviceName = "HelloClientService";            String portName = "HelloClient";            ServiceFactory serviceFactory = ServiceFactory.newInstance();            Service afService =serviceFactory.createService(new URL(wsdlUrl),         new QName(nameSpaceUri, serviceName));            HelloClientInterface proxy = (HelloClientInterface)        afService.getPort(new QName(                    nameSpaceUri, portName), HelloClientInterface.class);            System.out.println("return value is "+proxy.getName("john") ) ;        }catch(Exception ex)        {            ex.printStackTrace() ;        }    }}

4. Write WSDD to publish Web Services and write stub client to access web services

1. Compile the server program server, sayhello. Java, and compile server. sayhello. java.


      Package server; public class sayhello {Public String getname (string name) {return "hello" + name ;}} 2. write loghandler. javaimport Org. apache. axis. axisfault; import Org. apache. axis. handler; import Org. apache. axis. messagecontext; import Org. apache. axis. handlers. basichandler; import Java. util. date; public class loghandler extends basichandler {public void invoke (messagecontext msgcontext) throws axisfault {/** log an access each time we get invoked. */try {handler servicehandler = msgcontext. getservice (); integer numaccesses = (integer) servicehandler. getoption ("accesses"); If (numaccesses = NULL) numaccesses = new INTEGER (0); numaccesses = new INTEGER (numaccesses. intvalue () + 1); Date = new date (); string result = date + ": Service" + msgcontext. gettargetservice () + "accessed" + numaccesses + "time (s ). "; servicehandler. setoption ("accesses", numaccesses); system. out. println (result);} catch (exception e) {Throw axisfault. makefault (e );}}}

3. Write a WSDD File


      deploy.wsdd<deployment xmlns="http://xml.apache.org/Axis/wsdd/"  xmlns:java="http://xml.apache.org/Axis/wsdd/providers/java">              

3. Copy the compiled file to axis_home/WEB-INF/classes, such as: D:/tomcat/webapps/axis/WEB-INF/classes

4. Publish a service:

Java org. Apache. axis. Client. adminclient deploy. WSDD

5. Generate the client stub File

A: method 1

Copy sayhello. Java to axis_home/and rename it sayhello. JWS,

Run the following command to survive the client stub


      java org.apache.Axis.wsdl.WSDL2Java -p client  http://localhost:8080/Axis/services/SayHello.jws?wsdl

B: method 2

Run the following command to generate sayhello. WSDL.


      java org.apache.Axis.wsdl.Java2WSDL-oSayHello.wsdl -lhttp://localhost:8080/Axis/services/SayHello -nsayhello server.SayHello

Run the following command to generate client stub:


      java org.apache.Axis.wsdl.WSDL2Java SayHello.wsdl  -p client

The generated stub client file list is:

1. sayhello. Java

2. sayhelloservice. java.

3. sayhelloservicelocator. Java

4. sayhellosoapbindingstub. Java

6. Compile and execute the client program.


      public class SayHelloClient{    public static void main(String[] args){        try{    SayHelloService service = new client.       SayHelloServiceLocator();          client.SayHello_PortType   client = service.getSayHello();            String retValue=client.getName("zhangsan");            System.out.println(retValue);} catch (Exception e){ System.err.println ("Execution failed. Exception: " + e);        }    }}

(T117)

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 428537

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.