2. Use wsdd (Web Services Deployment Descriptor) to publish WebService

Source: Internet
Author: User

The wsdd method is a little more complex than the jws method, but to some extent it is more flexible than the jws release method.

1. First, create a java class in the AxisWebService project, and write the simple two methods getName and getAge. The Code is as follows:

 

Code

 Package com. hoo. service;

/**
* <B> function: </B> axis WebService in wsdd release Mode
* @ Author hoojo
* @ CreateDate Dec 15,201 0 17:24:35
* @ File HelloWorldWSDD. java
* @ Package com. hoo. service
* @ Project AxisWebService
* @ Blog http://blog.csdn.net/IBM_hoojo
* @ Email hoojo_@126.com
* @ Version 1.0
*/
Public class HelloWorldWSDD {

Public String getName (String name ){
Return "your name:" + name;
}

Public int getAge (int age ){
Return age + 10;
}
}

 

 

 

2. If the wsdd method is used, first we need to customize our wsdd xml file. Here we name it deploy. wsdd, which is created under the WEB-INF directory of the current web project, the Code is as follows:

 

Code

 <? 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">
<Service name = "HelloWorldWSDD" provider = "java: RPC">
<Parameter name = "className" value = "com. hoo. service. HelloWorldWSDD"/>
<! -- * Indicates that all methods are exposed -->
<Parameter name = "allowedMethods" value = "*"/>
<Parameter name = "scope" value = "request"/>
</Service>
</Deployment>

 

 

 

The service tag represents a WebService, and HelloWorldWSDD is the name of the current WebService; provider is the java WebService type, including RPC, Document, Wrapped, Message, EJB, and RMI; for more information, see org. apache. axis. providers. implementation class or document of the WebService under the java package;

The parameter className of parameter represents the class path of the current WebService;

AllowedMethods indicates the exposed methods that can be called on the client;

<Parameter name = "scope" value = "request"/>

This is the scope of the current WebService. It has three values: request, session, and application.

Request indicates that a service object is generated for each WebService SOAP request. It is similar to Spring's scope and consumes a lot of resources when service requests are frequent.

Session creates a service object for each client that calls the current WebService.

Application is a service object created for all current requests.

 

 

3, after writing the configuration, you need to use the AdminClient tool class provided by axis to help us publish WebService until the server-config.wsdd is generated, the steps are as follows: run the cmd command, and then enter the directory of the current project release, % tomcat_home %/webapps/project/WEB-INF>

My is: C: \ SoftWare \ tomcat-5.0.28 \ tomcat-5.0.28 \ webapps \ AxisWebService \ WEB-INF>

Run the following command: java-Djava. ext. dirs = lib org. apache. axis. client. AdminClient deploy. wsdd.

Deploy here. wsdd is the wsdd file we just customized. java is of course a jvm command,-Djava. ext. dirs = lib: sets the dependency package of the current command. AdminClient is a tool class provided by axis. This class can be directly run by admin in the official project (not here, the downloaded AdminServlet is missing. If you are interested, you can study it as an official example );

If you see:

Processing file deploy. wsdd

<Admin> Done processing </Admin>

It means it's almost successful, why? Fast! Check whether the directory at the same level of deploy. wsdd has generated a server-config.wsdd. If this file exists, it will succeed. If not, it will fail. If the problem persists, start tomcat and release the project. Then, repeat the preceding command. If the command fails, change the command line Code as follows:

Java-Djava. ext. dirs = lib org. apache. axis. client. AdminClient-lhttp: // localhost: 8080/AxisWebService/services/AdminService deploy. wsdd

-Lhttp: // localhost: 8080/AxisWebService/services/AdminService: Because your port may be occupied by axis, we will specify AdminService to complete the conversion, run the preceding command.

 

 

4. Enter in the address bar of WebBrowser:

Http: /localhost: 8080/AxisWebService/servlet/AxisServlet

You can see the following results:

And now... Some Services
  • HelloWorldWSDD(Wsdl)

    • GetName
    • GetAge
  • AdminService(Wsdl)
    • AdminService
  • Version(Wsdl)
    • GetVersion

The WebService specified in deploy. wsdd appeared on the page and exposed methods. Click "wsdl" to view the xml document of the same familiar wsdl.

 

5. Below we write the client code to call the WebService above. The code block is almost the same as the jws above, but the parameter and method name are different.

 

Code

 Package com. hoo. client;

Import java. rmi. RemoteException;
Import javax. xml. namespace. QName;
Import javax. xml. rpc. ServiceException;
Import org. apache. axis. client. Call;
Import org. apache. axis. client. Service;

/**
* <B> function: </B> axis WebService client in wsdd Mode
* @ Author hoojo
* @ CreateDate Dec 15,201 0 17:30:48
* @ File HelloWorldWSDDClient. java
* @ Package com. hoo. client
* @ Project AxisWebService
* @ Blog http://blog.csdn.net/IBM_hoojo
* @ Email hoojo_@126.com
* @ Version 1.0
*/
Public class HelloWorldWSDDClient {

Public static void main (String [] args) throws ServiceException, RemoteException {
// WebService access address
String url = "http: // localhost: 8080/AxisWebService/services/HelloWorldWSDD ";
// Create a service
Service service = new Service ();
// Create a call handle
Call call = (Call) service. createCall ();
// Set the request address
Call. setTargetEndpointAddress (url );
/**
* Set the namespace of the called methods and methods;
* Of course, null is also acceptable, because it does not set a namespace. The general namespace of the method is
* Package name inverted composition, such as com. hoo. service, ns = http://service.hoo.com
*/
Call. setOperationName (new QName ("http://service.hoo.com", "getName "));

/**
* Call the getName method and set the request parameters. The returned value is the returned value.
*/
String result = (String) call. invoke (new Object [] {"jack "});
System. out. println (result );

Call. setOperationName (new QName ("http://service.hoo.com", "getAge "));

/**
* Call the getAge method and set the request parameters. The returned value is the returned value.
*/
Int resultAge = Integer. parseInt (call. invoke (new Object [] {89}). toString ());
// Server + 10
System. out. println (resultAge );
}
}

 

 

 

The result is as follows:

Your name: jack

99

 

Call. setOperationName (new QName ("http://service.hoo.com", "getName "));

Here, the namespace can also be set with null, but it cannot be set at will. Sometimes an exception occurs.

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.