Simple example of WebService development

Source: Internet
Author: User
Tags date integer interface net string stub web services access
Web axis supports the deployment and development of three Web service types, respectively:

1, Dynamic invocation Interface (DII)

2, stubs way

3. Dynamic Proxy method

Ii. writing dii (Dynamic invocation Interface) Web Services

1. Writing service-side programs Helloclient

public class Helloclient
public string GetName (string name)
Return "Hello" +name;

2, the source code copy to Axis_home, renamed to Helloclient.jws

3, Access connection http://localhost:8080/Axis/HelloClient.jws?wsdl, the page shows Axis automatically generated WSDL

4, write access to the service client

Import Org.apache.Axis.client.Call;
Import Org.apache.Axis.client.Service;
Import Javax.xml.namespace.QName;
Import javax.xml.rpc.ServiceException;
Import java.rmi.RemoteException;

public class SayHelloClient2
public static void Main (string[] args)
String endpoint =

Service service = new service ();
Call call = NULL;

Call = (call) Service.createcall ();

Call.setoperationname (New QName (
(New (endpoint));

string ret = (string) call.invoke (new object[]
System.out.println ("return value is" + ret);
catch (Exception ex)
Ex.printstacktrace ();

Iii. writing dynamic proxy access Services

1, write the deployment of server-side programs, with the top dii mode, this time still use the top deployed helloclient

2, write Agent interface

public interface Helloclientinterface
Extends Java.rmi.Remote
public string GetName (string name)
Throws Java.rmi.RemoteException;

3, write and execute the client program

Import Javax.xml.rpc.Service;
Import Javax.xml.rpc.ServiceFactory;
Import Javax.xml.namespace.QName;

public class Testhelloclient
public static void Main (string[] args)
String Wsdlurl =
String NamespaceURI =
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),
("Return value is" +proxy.getname ("John"));
}catch (Exception ex)
Ex.printstacktrace ();

Iv. Writing WSDD Publishing Web services, writing stub client Access Web services

1, write the service-side program Server,, compile

Package server;
public class SayHello
public string GetName (string name)
Return "Hello" +name;
2. Preparation of
Import 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 all 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 date = new Date ();
String result =
Date + ": Service" +
Msgcontext.gettargetservice () +
"Accessed" + numaccesses + "time (s).";
("accesses", numaccesses);
catch (Exception e)
Throw Axisfault.makefault (e);

3, the preparation of WSDD documents

<deployment xmlns=
"Http://" >
<service name= "SayHello"
Provider= "Java:rpc" >
<parameter name= "ClassName"
Value= "server. SayHello "/>
<parameter name= "Allowedmethods"
Value= "*"/>

3, copy the compiled files to axis_home/web-inf/classes, such as: D:\tomcat\webapps\Axis\WEB-INF\classes

4, publishing services:

Java org.apache.Axis.client.AdminClient DEPLOY.WSDD

5. Generate Client stub files

A: Mode 1

Copy to axis_home/, rename to Sayhello.jws,

Execute the following command to survive the client stub

Java Org.apache.Axis.wsdl.WSDL2Java
-P Client http://localhost:8080

B: Mode 2

Execute the following command to generate SAYHELLO.WSDL

Java Org.apache.Axis.wsdl.Java2WSDL
/axis/services/sayhello-nsayhello server. SayHello

Execute the following command to generate the client stub

Java Org.apache.Axis.wsdl.WSDL2Java
Sayhello.wsdl-p Client

The generated stub client file list is:

6, write the client program, compile and execute

public class Sayhelloclient
public static void Main (string[] args)
Sayhelloservice service = new client.
Sayhelloservicelocator ();
Client. Sayhello_porttype
Client = Service.getsayhello ();
String retvalue=client.getname ("Zhangsan");
System.out.println (RetValue);
catch (Exception e)
("Execution failed.") Exception: "+ e);

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: 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.