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 Testhelloclient.java


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 ();
}
}
}




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 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 ();
}
}
}




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

1, write the service-side program Server,sayhello.java, compile Server.SayHello.java


Package server;
public class SayHello
{
public string GetName (string name)
{
Return "Hello" +name;
}
}
2. Preparation of Loghandler.java
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).";
Servicehandler.setoption
("accesses", numaccesses);
SYSTEM.OUT.PRINTLN (result);
catch (Exception e)
{
Throw Axisfault.makefault (e);
}
}
}




3, the preparation of WSDD documents


Deploy.wsdd
<deployment xmlns=
"Http://xml.apache.org/Axis/wsdd/"
Xmlns:java=
"Http://xml.apache.org/Axis/wsdd/providers/java" >
<service name= "SayHello"
Provider= "Java:rpc" >
<requestFlow>
</requestFlow>
<parameter name= "ClassName"
Value= "server. SayHello "/>
<parameter name= "Allowedmethods"
Value= "*"/>
</service>
</deployment>




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 Sayhello.java 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
/axis/services/sayhello.jws?wsdl




B: Mode 2

Execute the following command to generate SAYHELLO.WSDL


Java Org.apache.Axis.wsdl.Java2WSDL
-osayhello.wsdl-lhttp://localhost:8080
/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:

1.sayhello.java

2.sayhelloservice.java.

3.sayhelloservicelocator.java

4.sayhellosoapbindingstub.java

6, write the client program, compile and execute


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);
}
}
}

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.