Java WebService instance

Source: Internet
Author: User
I. axis installation 1. Environment j2se SDK 1.5 or 1.6: I use 1.6 servlet container: I use Tomcat 6.0

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

3. Create a web project under eclipse as webservicedemo. in the xml configuration of Tomcat, <context Path = "/WebService" docbase = "D: \ workspace \ webservicedemo \ webroot"/>. decompress the downloaded axis installation package and paste the Lib folder and web under webapps \ axis. copy the XML file to the project directory

4. start Tomcat and access http: // localhost/webservicecheck that the installation of ingress is successful. If my port is set to 80, error occurs after page index.html jumps automatically because the class ingress is not input.

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. Create the com. qdl. server package and write the server program sayhello.
Package com. qdl. server;
Public class sayhello
{
Public String getname (string name)
{
Return "hello" + name;
}
}

2. Copy the source code to webroot and rename it to sayhello. JWS (do not copy the package... line above)

3. Access http: // localhost/WebService/sayhello. JWS? WSDL, the page displays the axis auto-generated WSDL, and the secure sclasses folder is created under the WEB-INF where the sayhello. Class file is generated

4. Create the com. qdl. Client package and write the client sayhelloclient. Java that accesses the service.

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 ){
// The program accesses the instant Publishing Service
Try {
// Create an access point
String endpoint = "http: // localhost/WebService/sayhello. JWS ";
// Create a service
Service = new service ();
Call call = NULL;
// Create a call
Call = (CALL) service. createcall ();
// Set the operation name
Call. setoperationname (New QNAME ("http: // localhost/WebService/sayhello. JWS", "getname "));
// Set the Access Point
Call. settargetendpointaddress (New java.net. URL (endpoint ));
// Call the service, Return Value
String ret = (string) Call. Invoke (new object [] {"zhangsan "});
System. Out. println ("returned information:" + 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 above-mentioned DiI mode. This time, sayhello is still used.

2. Create a new package com. qdl. DAO to write the proxy Interface

Package com. qdl. Dao;

Import java. RMI. Remote; public interface helloclientinterface extends remote {

Public String getname (string name) throws java. RMI. RemoteException;
}
3. Write and execute the client program testhelloclient. Java

Package com. qdl. client;

Import java.net. url; import javax. xml. namespace. QNAME;
Import javax. xml. rpc. servicefactory; import javax. xml. rpc. Service; import com. qdl. Dao. helloclientinterface; public class testhelloclient {
Public static void main (string [] ARGs ){
Try {
String wsdlurl = "http: // localhost/WebService/sayhello. JWS? WSDL ";
String namespaceuri = "http: // localhost/WebService/sayhello. JWS ";
// Note the following servicename and portname,
// Http: // localhost/WebService/sayhello. JWS? At the bottom of XML on the WSDL page
String servicename = "sayhelloservice ";
String portname = "sayhello ";
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 ("returned information:" + proxy. getname ("zhangsan "));
} Catch (exception ex ){
Ex. printstacktrace ();
}
}}

4. Write WSDD to publish Web Services and write stub client to access web services (custom publishing services)

1. Write the server program sayhello.
Package com. qdl. server;
Public class sayhello
{
Public String getname (string name)
{
Return "hello" + name;
}
}
2. Write loghandler. Java (it seems this is useless)
Import java. util. date; import org. Apache. axis. axisfault;
Import org. Apache. axis. Handler;
Import org. Apache. axis. messagecontext;
Import org. Apache. axis. Handlers. basichandler; public class loghandler extends basichandler {
Public void invoke (messagecontext msgcontext) throws axisfault {
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, first in the WEB-INF directory to write the server-config.wsdd file:

File Name server-config.wsdd: <? XML version = "1.0" encoding = "UTF-8"?>
<Deployment xmlns = "http://xml.apache.org/axis/wsdd"
Xmlns: Java = "http://xml.apache.org/axis/wsdd/providers/java">
<Globalconfiguration>
<Parameter name = "adminpassword" value = "admin"/>
<Parameter name = "attachments. directory" value = "./attachments"/>
<Parameter name = "attachments. Implementation"
Value = "org. Apache. axis. attachments. attachmentsimpl"/>
<Parameter name = "sendxsitypes" value = "true"/>
<Parameter name = "sendmultirefs" value = "true"/>
<Parameter name = "sendxmldeclaration" value = "true"/>
<Parameter name = "axis. sendminimizedelements" value = "true"/>
<Requestflow>
<Handler type = "Java: org. Apache. axis. Handlers. mongoshandler">
<Parameter name = "Scope" value = "session"/>
</Handler>
<Handler type = "Java: org. Apache. axis. Handlers. mongoshandler">
<Parameter name = "Scope" value = "request"/>
<Parameter name = "extension" value = ". JWR"/>
</Handler>
</Requestflow>
</Globalconfiguration>
<Handler name = "localresponder"
Type = "Java: org. Apache. axis. Transport. Local. localresponder"/>
<Handler name = "urlmapper"
Type = "Java: org. Apache. axis. Handlers. http. urlmapper"/>
<Handler name = "authenticate"
Type = "Java: org. Apache. axis. Handlers. simpleauthenticationhandler"/>
<Service name = "adminservice" provider = "Java: MSG">
<Parameter name = "allowedmethods" value = "adminservice"/>
<Parameter name = "enableremoteadmin" value = "false"/>
<Parameter name = "classname" value = "org. Apache. axis. utils. admin"/>
<Namespace> http://xml.apache.org/axis/wsdd/ </namespace>
</Service>
<Service name = "version" provider = "Java: RPC">
<Parameter name = "allowedmethods" value = "getversion"/>
<Parameter name = "classname" value = "org. Apache. axis. Version"/>
</Service>
<Transport name = "HTTP">
<Requestflow>
<Handler type = "urlmapper"/>
<Handler type = "Java: org. Apache. axis. Handlers. http. httpauthhandler"/>
</Requestflow>
</Transport>
<Transport name = "local">
<Responseflow>
<Handler type = "localresponder"/>
</Responseflow>
</Transport>
</Deployment> then write the deploy. WSDD file in the WEB-INF:
File Name: deploy. WSDD
<Deployment xmlns = "http://xml.apache.org/axis/wsdd/" xmlns: Java = "http://xml.apache.org/axis/wsdd/providers/java">
<Service name = "myservice" provider = "Java: RPC">
<Parameter name = "classname" value = "com. qdl. server. sayhello"/>
<Parameter name = "allowedmethods" value = "*"/>
</Service>
</Deployment>
4. Publish a service:

Java org. Apache. axis. Client. adminclient-lhttp: // address: Port/context module/services/adminservice deploy. WSDD

This command mainly refers to deploy. WSDD service configuration to the server-config.wsdd, while updating the Memory note: the premise of the above command is to configure the jar package of axis to the system classpath environment variables, otherwise the error of class not found

5. Generate the client stub File

A: method 1 (I use this method)

Copy sayhello. Java to webroot (copied earlier) and rename it sayhello. JWS,

Run the following command to survive the client stub

Java org. Apache. axis. WSDL. wsdl2java-P com. qdl. Client http: // localhost/WebService/sayhello. JWS? WSDL

B: method 2

Run the following command to generate sayhello. WSDL.

Java org. Apache. axis. WSDL. java2wsdl-osayhello. WSDL-lhttp: // localhost/WebService/sayhello-nyoyhello com. qdl. server. sayhello

Run the following command to generate client stub:

Java org. Apache. axis. WSDL. wsdl2java sayhello. WSDL-P com. qdl. Client


The stub client file list generated under com. qdl. Client under the folder WEB-INF is:

1. sayhello. Java

2. sayhelloservice. java.

3. sayhelloservicelocator. Java

4. sayhellosoapbindingstub. Java

Cut the com package to SRC for compilation.

6. Compile and execute the client program.

Package com. qdl. client;

Public class sayhelloclient2 {
Public static void main (string [] ARGs ){
// Access the customized Publishing Service
Try {
Sayhelloservice service = new sayhelloservicelocator ();
Sayhello_porttype client = service. getsayhello ();
String retvalue = client. getname ("zhangsan ");
System. Out. println ("returned information:" + 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.