Here we have sorted out some key points for developing web services through axis, hoping that developers who are not familiar with Web services can quickly master web services.
1. Installation of the Axis Environment
1. Install j2se SDK 1.4, Tomcat 5.0, and eclipse 3.2.
2. Download the axis installation package from the http://xml.apache.org website.
3. Place the axis related package file in the WEB-INF/lib directory.
4, axis Optional package: Activation. jar; mail. jar; xmlsec-1.4.Beta1.jar copy to the WEB-INF directory, the client generation needs to use.
Axis supports the deployment and development of three web services:
1. dynamic invocation interface (DiI)
2. Dynamic proxy
3. stubs Mode
I will not introduce the first two methods. My colleagues told me that they did not use the first two methods. They suggested that we use the stubs method, so I will mainly introduce the third method. Note that my own Java source code is placed in the D:/workspace/test/directory, and the axis related package files are placed in the D:/workspace/test/WEB-INF directory.
2. 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 a WSDD File
The content of the deploy. WSDD file is as follows:
<Deployment xmlns = "http://xml.apache.org/axis/wsdd/" xmlns: Java = "http://xml.apache.org/axis/wsdd/providers/java">
<Service name = "sayhello" provider = "Java: RPC">
<Parameter name = "classname" value = "server. sayhello. getname"/>
<Parameter name = "allowedmethods" value = "*"/>
</Service>
</Deployment>
3. Publish a service:
Edit deploy. bat, and axis_lib is the axis. Jar path. The content is as follows:
Set axis_lib = D:/workspace/test/WEB-INF/lib
Set java_cmd = Java-djava. Ext. dirs = % axis_lib %
Set axis_servlet = http: // localhost: 8080/test/servlet/axisservlet
% Java_cmd % org. Apache. axis. Client. adminclient-l % axis_servlet % deploy. WSDD
Execute this batch file. If the prompt is successful, access http: // localhost: 8080/test/services to display the service list.
4. Generate the client stub file on the client.
Access the server-side services in a browser and download them to the WSDL file. Through the related tools of axis, the Web service client code can be automatically generated from the WSDL file.
Write a wsdl2java. BAT file with the following content:
Set axis_lib = D:/workspace/test/WEB-INF/lib
Set java_cmd = Java-djava. Ext. dirs = % axis_lib %
Set output_path = D:/workspace/test/src
Set package = server. sayhello
% Java_cmd % org. Apache. axis. WSDL. wsdl2java-o % output_path %-P % package % sayhello. WSDL
Execute this batch file to generate client stub.
The generated stub client file list is: sayhello. Java, sayhelloservice. Java, sayhelloservicelocator. Java, sayhellosoapbindingstub. java.
5. Compile and execute the client program.
Below is a piece of JUnit test client code.
Import java.net. url;
Import JUnit. Framework. test;
Import JUnit. Framework. testcase;
Import JUnit. Framework. testsuite;
Public class testwsclient extends testcase {
Public testwsclient (string ){
Super (string );
}
Public void sayhelloclient () throws exception {
Sayhelloservice service = new sayhelloservicelocator ();
Sayhello_porttype client = service. getsayhello ();
String retvalue = client. getname ("clientname ");
System. Out. println (retvalue );
}
Public Static Test Suite (){
Testsuite suite = new testsuite ();
Suite. addtest (New testwsclient ("sayhelloclient "));
Return suite;
}
}