After learning about WebService, SOAP, WSDL, and UDDI, let's start to make a small example of WebService. It turns out to be very difficult, but after that, let's take a look at it, it is the same as the RPC mentioned by the teacher in college. Is a remote call process, so easy ~~
Let's configure our environment first. Axis is an open-source Apache WebService Service and must be published by web containers. Here we often use Tomcat as a Java User. Oh, that's a coincidence. First, let's take a look at what needs to be configured in Tomcat (we all say that no configuration is required for the installation version. Well, if it is an installation-free version, I 'd like to leave it alone. When I used the free installation version for the first time, I clicked "startup. Bat failed" for more than an hour. I was so angry that I didn't have dinner in a day. It's useless. Let's look at the Configuration:
In the bin folder of the tomcat installation path, find startup. BAT (Startup File) and add the following two lines at the top of the file:
Set java_home = c: \ Program Files \ Java \ jdk1.6.0 _ 22 # JDK installation path
Set catalina_home = D: \ apache-Tomcat-6.0.32 # tomcat installation path
Simple. Double-click STARTUP. bat.
Next, configure axis. Assume that you have downloaded the axis package. Place the axis project in the Tomcat webapps directory.
Set the following variables in the system environment variables:
Catalina_home
D: \ apache-Tomcat-6.0.32
Axis_home
% Catalina_home % \ webapps \ axis
Axis_lib
% Axis_home % \ Lib
Axisclasspath
% Axis_lib % \ axis. jar; % axis_lib % \ commons-discovery.jar; % axis_lib % \ commons-logging.jar; % axis_lib % \ jaxrpc. jar; % axis_lib % \ SAAJ. jar; % axis_lib % \ log4j-1.2.8.jar; % axis_lib % \ xml-apis.jar; % axis_lib % \ xercesimpl. jar
Add the following to classpath:
% Axis_lib % \ axis. jar; % axis_lib % \ commons-discovery.jar; % axis_lib % \ commons-logging.jar; % axis_lib % \ jaxrpc. jar; % axis_lib % \ SAAJ. jar; % axis_lib % \ log4j-1.2.8.jar; % axis_lib % \ xml-apis.jar; % axis_lib % \ xercesimpl. jar
Now, let's test our axis:
Start the browser and enter http: // localhost: 8080/axis/happyaxis. jsp. If you see the following content displayed on the webpage:
View code
Axis Happiness Page
Examining webapp configuration
Needed Components
Found SAAJ API (javax.xml.soap.SOAPMessage) at an unknown location
Found JAX-RPC API (javax.xml.rpc.Service) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\jaxrpc.jar
Found Apache-Axis (org.apache.axis.transport.http.AxisServlet) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\axis.jar
Found Jakarta-Commons Discovery (org.apache.commons.discovery.Resource) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\commons-discovery.jar
Found Jakarta-Commons Logging (org.apache.commons.logging.Log) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\commons-logging.jar
Found Log4j (org.apache.log4j.Layout) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\log4j-1.2.8.jar
Found IBM's WSDL4Java (com.ibm.wsdl.factory.WSDLFactoryImpl) at D:\apache-tomcat-6.0.32\webapps\axis\WEB-INF\lib\wsdl4j.jar
Found JAXP implementation (javax.xml.parsers.SAXParserFactory) at an unknown location
Found Activation API (javax.activation.DataHandler) at an unknown location
Optional Components
Warning: could not find class javax.mail.internet.MimeMessage from file mail.jar
Attachments will not work
See http://java.sun.com/products/javamail/
Warning: could not find class org.apache.xml.security.Init from file xmlsec.jar
XML Security is not supported
See http://xml.apache.org/security/
Found Java Secure Socket Extension (javax.net.ssl.SSLSocketFactory) at an unknown location
The core axis libraries are present. 2 optional axis libraries are missing
Note: On Tomcat 4.x and Java1.4, you may need to put libraries that contain java.* or javax.* packages into CATALINA_HOME/common/lib
jaxrpc.jar and saaj.jar are two such libraries.
Note: Even if everything this page probes for is present, there is no guarantee your web service will work, because there are many configuration options that we do not check for. These tests are necessary but not sufficient
Examining Application Server
Servlet version 2.5
XML Parser com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl
XML ParserLocation an unknown location
Examining System Properties
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jdk1.6.0_22\jre\bin
java.vm.version=17.1-b03
shared.loader=
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
tomcat.util.buf.StringCache.byte.enabled=true
file.encoding.pkg=sun.io
java.util.logging.config.file=D:\apache-tomcat-6.0.32\conf\logging.properties
user.country=CN
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=Service Pack 3
java.vm.specification.name=Java Virtual Machine Specification
user.dir=D:\apache-tomcat-6.0.32\bin
java.runtime.version=1.6.0_22-b04
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=D:\apache-tomcat-6.0.32\endorsed
os.arch=x86
java.io.tmpdir=D:\apache-tomcat-6.0.32\temp
line.separator=
java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
java.util.logging.manager=org.apache.juli.ClassLoaderLogManager
java.naming.factory.url.pkgs=org.apache.naming
os.name=Windows XP
sun.jnu.encoding=GBK
java.library.path=C:\Program Files\Java\jdk1.6.0_22\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\PROGRA~1\Borland\CBUILD~1\Bin;D:\PROGRA~1\Borland\CBUILD~1\Projects\Bpl;C:\Program Files\PC Connectivity Solution\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;C:\Program Files\Java\jdk1.6.0_22\bin;E:\resource\android-sdk-windows\tools
java.specification.name=Java Platform API Specification
java.class.version=50.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\Administrator
user.timezone=Asia/Shanghai
catalina.useNaming=true
java.awt.printerjob=sun.awt.windows.WPrinterJob
java.specification.version=1.6
file.encoding=GBK
catalina.home=D:\apache-tomcat-6.0.32
user.name=Administrator
java.class.path=D:\apache-tomcat-6.0.32\bin\bootstrap.jar
java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory
package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=C:\Program Files\Java\jdk1.6.0_22\jre
java.specification.vendor=Sun Microsystems Inc.
user.language=zh
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.6.0_22
java.ext.dirs=C:\Program Files\Java\jdk1.6.0_22\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
sun.boot.class.path=C:\Program Files\Java\jdk1.6.0_22\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_22\jre\classes
java.vendor=Sun Microsystems Inc.
server.loader=
catalina.base=D:\apache-tomcat-6.0.32
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.,sun.beans.
sun.desktop=windows
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
Platform: Apache Tomcat/6.0.32
Congratulations, the configuration is successful. Below we will write a simple WebService Interface and try it through a client call:
Create a Java file named sayhello. Java in our axis project. The file content is as follows:
public class SayHello {
public String getName(String name) {
return "hello "+name;
}
}
Don't laugh. It's that simple.Axis provides two service publishing methods: instant deployment and custom deployment ).Isn't that awkward? I mean
I would like to emphasize that the first release method is used here. This method is simple. The two are not analyzed temporarily. We just want to start with something simple.
Access the following http: // localhost: 8080/axis/sayhello. JWS? WSDL. We can see that our WSDL is directly generated in this simple way.
Next we will write a simple client on the client to access our WebService. sayhelloclient. Java is as follows:
View code
package com.jftt.soap;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class SayHelloClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/axis/SayHello.jws";
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName("http://localhost:8080/axis/SayHello.jws",
"getName"));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
String ret = (String) call.invoke(new Object[] {"changjianlong"});
System.out.println("return value is " + ret);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Import the missing package ---- the packages used here are included in the axis package we downloaded. You can find them. If not, we will introduce all the packages in the Lib of axis. Haha