WebService's HelloWorld, the server side and the client's demo (GO)----themselves to build Web project, not web Service project, and use WSDD to publish

Source: Internet
Author: User
Tags soap log log wsdl

WebService HelloWorld, server-side vs. client demo

http://blog.csdn.net/angus_17/article/details/8032856

The sudden rise of today, want to learn webservice, and then look for information, did a simple demo. Record it for later use.

First of all, first of all, say something theoretically.

WebService is an advanced application that differs from struts, Spring, hibernate, and other frameworks that were previously learned. WebService is a service-oriented architecture (SOA) that looks like it is larger than the SSH framework. So what exactly is it for? What is a service-oriented architecture?

Let's look at a requirement that a group of companies may have a variety of Web applications. For example, the year before the development of a invoicing system, developed a ERP, this year, and developed an OA. Now that the group needs to integrate these three systems, does it need to be re-coded to integrate them? And these three systems are written in different languages, this cost to the company is undoubtedly a waste. WebService can be a good solution to this demand.

WebService is a solution that can be integrated across languages, across platforms, and distributed systems, webservice like a line that wears these systems-an enterprise service bus (ESB). WebService uses the Simple Object Access Protocol (SOAP) to transfer XML data using the HTTP protocol (XML is the most common and other format data.) ) to complete the integration between the belts.

What is consolidation? Of course, the integration of functions and data, that is, one system can invoke the WebService interface of another system to complete the data interaction. In this way we need to know which interfaces are exposed by the application that provides the WebService service functionality, which we can learn through the WebService description document (WSDL). WSDL does not need to be written manually, Java's WebService implementation can be generated for us automatically . JDK1.6 new support WebService, but not mature enough. So we use the webservice provided by the Apache third party open source organization to implement--axis.

The current version of axis is the Java version, and its C + + version is under development. Axis is a powerful soap engine, and details about them are not introduced here. Below, let's write an example program to understand the WebService application process.

=============================================================================================================== ===

Start writing the service-side code below:

1. In MyEclipse, establish a webproject. Name is WebService.

2. Download the axis1.4 package, unzip it, there are some information and documents about axis.

Copy the Web. Xml from the WebApps inside, Web-inf, to your own WebService project.

If you do not, you can copy the following code into your Web. xml:

<?xml version= "1.0" encoding= "Iso-8859-1"?>

<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD web
Application 2.3//en "" Http://java.sun.com/dtd/web-app_2_3.dtd ">

<web-app>
<display-name>Apache-Axis</display-name>

<listener>
<listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
</listener>

<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>apache-axis servlet</display-name>
<servlet-class>
Org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>

<servlet>
<servlet-name>AdminServlet</servlet-name>
<display-name>axis Admin servlet</display-name>
<servlet-class>
Org.apache.axis.transport.http.AdminServlet
</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>

<servlet>
<servlet-name>SOAPMonitorService</servlet-name>
<display-name>SOAPMonitorService</display-name>
<servlet-class>
Org.apache.axis.monitor.SOAPMonitorService
</servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name>
<param-value>5001</param-value>
</init-param>
<load-on-startup>100</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>SOAPMonitorService</servlet-name>
<url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>

<!--Uncomment this if you want the admin servlet--
<!--
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
-

<session-config>
<!--Default to 5 minute session timeouts--
<session-timeout>5</session-timeout>
</session-config>

<!--currently the havent settled on a media type for WSDL;
http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
For now, we go with the basic ' it ' XML ' response--
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>

<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>

<welcome-file-list id= "Welcomefilelist" >
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jws</welcome-file>
</welcome-file-list>
</web-app>

Then, the jar package is introduced,

In your own axis jar, there are some jar packages that can be introduced into all of them.

I only introduced a part of it axis.jar,axis-ant.jar,commons-discovery-0.2.jar,commons-logging-1.0.4.jar,jaxrpc.jar,log4j-1.2.8.jar,saaj.jar,w Sdl4j-1.5.1.jar,xercesimpl-2.8.1.jar,xmlsec-1.4.5.jar.

Then, deploy the project, start Tomcat, enter Http://10.64.59.12:8080/WebService/services, and view the page.

If there is a

And now ... Some Services
    • Adminservice (WSDL)
      • Adminservice
    • Version (WSDL)
      • GetVersion

It means that you have completed the construction of the environment. Can enter the real development.

3. In SRC, the build class: Helloservice.java

public class HelloService {
Public String SayHello () {
return "Hello";
}

public string Sayhellotoperson (string name) {
if (name = = "" | | Name.equals ("")) {
Name = "Nobody";
}
Return "Hello" + name;
}
}

4. In fact, the development work has been completed, so simple. The next step is to publish this to WebService.

Write a file: DEPLOY.WSDD

The contents are as follows:

<?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= "helloservices" provider= "Java:rpc" >
<parameter name= "ClassName" value= "HelloService"/>//If the class is placed under a package, value should be written in packagename.classname form
<parameter name= "Allowedmethods" value= "*"/>
</service>
</deployment>

Put it under the F:\test,

Then cmd, enter F:test, and then enter:

JAVA-CP F:\axis-1_4\lib\axis.jar; F:\axis-1_4\lib\axis-ant.jar; F:\axis-1_4\lib\commons-discovery-0.2.jar; F:\axis-1_4\lib\commons-logging-1.0.4.jar; F:\axis-1_4\lib\jaxrpc.jar; F:\axis-1_4\lib\log4j-1.2.8.jar; F:\axis-1_4\lib\mail-1.4.jar; F:\axis-1_4\lib\saaj.jar; F:\axis-1_4\lib\wsdl4j-1.5.1.jar; F:\axis-1_4\lib\xercesImpl-2.8.1.jar; F:\axis-1_4\lib\xml-apis-1.3.03.jar; F:\axis-1_4\lib\xmlsec-1.4.5.jar; F:\axis-1_4\lib\xmlsecSamples-1.4.5.jar; F:\axis-1_4\lib\xmlsecTests-1.4.5.jar org.apache.axis.client.adminclient-lhttp://10.64.59.12:8080/ Webservice/services/adminservice DEPLOY.WSDD

(Your own process of writing:


Enter, which appears below the window:

Processing file DEPLOY.WSDD
<admin>done processing</admin>

It means you've made a successful release.

When you enter http://10.64.59.12:8080/WebService/services in the browser, you see

And now ... Some Services
    • Helloservices (WSDL)
      • SayHello
      • Sayhellotoperson
    • Adminservice (WSDL)
      • Adminservice
    • Version (WSDL)
      • GetVersion

The Helloservices was successfully released.

=============================================================================================================== ====================

The following is the client's access to WebService

Write a test class:

Import java.io.IOException;
Import java.net.MalformedURLException;
Import Javax.xml.namespace.QName;
Import javax.xml.rpc.ServiceException;
Import Org.apache.axis.client.Call;
Import Org.apache.axis.client.Service;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;


public class Testservice {
Private static final Log log = Logfactory.getlog (Testservice.class);
private static final String Hello_service_endpoint = "http://10.64.59.12:8080/WebService/services/HelloServices?wsdl ";

/**
* Call the SayHello method
*/
public void Callsayhello () {
try {
Service service = new service ();
Call Call Service.createcall ();
Delivery URL
Call.settargetendpointaddress (New Java.net.URL (Hello_service_endpoint));
Call Method Name
Call.setoperationname (New QName ("http://webservice.sinosoft.com/", "SayHello"));
Call.setreturntype (Org.apache.axis.Constants.XSD_STRING);
try {
RET is the return value obtained after executing the method, where no arguments are used, so no strings are written
string ret = (string) call.invoke (new object[] {});
System.out.println ("The return value is:" + ret);
Return
} catch (IOException e) {
E.printstacktrace ();
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (Serviceexception e) {
E.printstacktrace ();
}
Log.error ("Call SayHello service error!");
}

/**
* Call the Sayhellotoperson method
*/
public void Callsayhellotoperson () {
try {
Service service = new service ();
Call Call Service.createcall ();
Call.settargetendpointaddress (New Java.net.URL (Hello_service_endpoint));
Call.setoperationname (New QName ("http://webservice.sinosoft.com/", "Sayhellotoperson"));
Call.addparameter ("name", Org.apache.axis.Constants.XSD_STRING, Javax.xml.rpc.ParameterMode.IN);
Call.setreturntype (Org.apache.axis.Constants.XSD_STRING);
try {
RET is the execution method after the resulting return value, where a string is passed as a parameter, if not write empty, will error
string ret = (string) call.invoke (new object[] {"Seven"});
System.out.println ("The return value is:" + ret);
Return
} catch (IOException e) {
E.printstacktrace ();
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (Serviceexception e) {
E.printstacktrace ();
}
Log.error ("Call SayHello service error!");
}

/**
* Call two methods
* @param args
*/
public static void Main (string[] args) {
Testservice tester = new Testservice ();
Tester.callsayhello ();
Tester.callsayhellotoperson ();
}
}

Will print out:

The return value Is:hello
The return value Is:hello Seven

It means the demo has been successful.

At this point, if you modify Helloservice.java, when you execute the test class, you can instantly respond to changes in the Helloservice.java.

WebService's HelloWorld, the server side and the client's demo (GO)----themselves to build Web project, not web Service project, and use WSDD to publish

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.