WebService Implementation Method Using pojo class

Source: Internet
Author: User

1. axis installation 1. j2se SDK 1.3 or 1.4: I use 1.4.2 servlet container: Tomcat 5.0

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

3. decompress the installation package and copy the axis package under axis_unzip_path/axis-version/webapps to tomcat_home/webapps/. The axis_home directory is the tomcat_home/webapps/axis directory.

4. start Tomcat and access http: // localhost: 8080/axis to check whether the installation is successful.

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. Write the server program helloclient

Public class helloclient
{
Public String getname (string name)
{
Return "hello" + name;
}
}

2. Copy the source code to axis_home and rename it helloclient. JWS.

3. Access http: // localhost: 8080/axis/helloclient. JWS? WSDL: The page displays the WSDL automatically generated by axis.

4. Write the client testhelloclient. Java to access the service.

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

3. Write dynamic proxy to access the service

1. Write and deploy the server program in the same way as the DiI method on the same side. The helloclient deployed above is still used this time.

2. Compile the proxy 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 =
& Quot; http: // localhost: 8080/axis/helloclient. JWS? WSDL ";
String namespaceuri =
"Http: // localhost: 8080/axis/helloclient. JWS ";
String servicename = "helloclientservice ";
String portname = "helloclient ";

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

4. 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 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 each 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 = 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. Write a WSDD File

Deploy. WSDD
<Deployment xmlns =
Http://xml.apache.org/Axis/wsdd"
Xmlns: Java =
Http://xml.apache.org/Axis/wsdd/providers/java>
<Handler name = "print" type = "Java: loghandler"/>
<Service name = "sayhello"
Provider = "Java: RPC">
<Requestflow>
<Handler type = "print"/>
</Requestflow>
<Parameter name = "classname"
Value = "server. sayhello"/>
<Parameter name = "allowedmethods"
Value = "*"/>
</Service>
</Deployment>

3. Copy the compiled file to axis_home/WEB-INF/classes, such as: D:/tomcat/webapps/axis/WEB-INF/classes

4. Publish a service:

Java org. Apache. axis. Client. adminclient deploy. WSDD

5. Generate the client stub File

A: method 1

Copy sayhello. Java to axis_home/and rename it sayhello. JWS,

Run 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: method 2

Run the following command to generate sayhello. WSDL.

Java org. Apache. axis. WSDL. java2wsdl
-Osayhello. WSDL-lhttp: // localhost: 8080
/Axis/services/sayhello-nginx Hello server. sayhello

Run the following command to generate 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. Compile and execute the client program.

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

You can also write 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. 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"/>
<Handler name = "print" type = "Java: stub. loghandler"/>

<Service name = "sayhello" provider = "Java: RPC">
<Requestflow>
<Handler type = "print"/>
</Requestflow>
<Parameter name = "classname" value = "stub. server. sayhello"/>
<Parameter name = "allowedmethods" value = "*"/>
</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>
This is easier to use.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/madun/archive/2009/01/14/3777434.aspx

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.