WebService Tutorial (a) _java

Source: Internet
Author: User
Tags http post soap readable wsdl

Web Services can convert an application to a network application.

By using Web Services, your application can publish information to the world or provide a feature.

Web Services can be used by other applications.

With Web Services, your accounting department's Win 2k server can connect to your IT vendor's UNIX servers.

The basic Web Services platform is xml+http.

WEB Services uses XML to decode data and use SOAP to transmit data.

What is WebService

WebService, as its name suggests, is a web-based service. It uses the Web (HTTP) method to receive and respond to some kind of request from an external system. To implement remote invocation.

1: From the WebService work mode of understanding, it and ordinary Web programs (such as ASP, JSP, etc.) and there is no essential difference, are based on the HTTP Transport Protocol program.

The data used by 2:webservice is based on XML format. The current standard WebService uses SOAP protocols primarily in the data format. The SOAP protocol is actually a text protocol based on XML Coding specification.

Technical support of WebService

The Web service platform requires a set of protocols to implement the creation of distributed applications. Any platform has its data representation method and type system. To achieve interoperability, the WEB service platform must provide a standard set of type systems for communicating different types of systems in different platforms, programming languages, and component models. At present these agreements are:

XML and XSD

Extensible Markup Language XML is the basic format for representing data in a Web service platform. In addition to being easy to build and easily analyze, the main advantage of XML is that it is neither platform independent nor vendor independent. XML was created by the World Wide Web Consortium (WWW), and the XML schemaxsd defined a set of standard data types, and a language was developed to extend this set of data types.

The WEB service platform is used as a data type system using XSD. When you use a language such as VB. NET or C # to construct a Web service, all of the data types you use must be converted to XSD types in order to conform to the Web service standards. If you want it to be passed between different organizations on different platforms and different software, you need to wrap it up with something. This kind of thing is a protocol, like SOAP.

Soap

Soap is a Simple Object Access Protocol (protocal), which is a lightweight protocol for exchanging XML encoded information. It has three main aspects: Xml-envelope defines a framework for describing information content and how to handle content, encoding program objects into rules for XML objects, and executing remote Procedure call (RPC) conventions. Soap can run on any other transport protocol. For example, you can use SMTP, the Internet e-mail protocol, to deliver SOAP messages, which is tempting. The headers between the transport layers are different, but the XML payload remains the same.

Web Service wants to implement a "software-software conversation" approach between different systems, breaking the state of incompatibility between software applications, Web sites and various devices, and achieving the goal of "seamless integration on the Web".

Wsdl

Web Service Description Language WSDL is an XML-based language for describing Web service and its functions, parameters, and return values, which is provided in a machine-readable way. Because it is xml-based, WSDL is both machine readable and human readable.

Uddi

The purpose of UDDI is to establish standards for E-commerce; UDDI is a web-based, distributed, implementation-standard specification for Web service-information registries, as well as a set of Web service providers that enable businesses to register themselves. To enable other enterprises to discover the implementation criteria of the Access Protocol. Calling RPC and message passing

The WEB service itself is actually implementing communication between applications. We now have two methods of application communication: RPC Remote procedure call and message delivery. When using RPC, the concept of the client is to invoke a remote procedure on the server, typically by instantiating a remote object and calling its methods and properties. The RPC system attempts to achieve a position of transparency: the server exposes the interface of the remote object, and the client is like the interface of these objects used locally, thus hiding the underlying information, and the client does not need to know which machine the object is on.

How to publish a webservice?

1, with Jdk1.6.0_21 later release a WebService service. and view its WSDL document through the address bar.

2, generate the client code through Wsimport, call and view the results of the run. (Learn how to call is our focus).

It is important to note that jdk1.6._07 after the JDK release WebService must be a complete annotation of the code, if the use of jdk1.6.0_21 later version, because it already contains ws2.1 so, you can only add @webservice annotations to the class .

Here are two different pieces of code:

WS published on jdk1.6.0_13 version:

Package com.itcast; 
Import Javax.jws.WebMethod; 
Import Javax.jws.WebService; 
Import javax.jws.soap.SOAPBinding; 
Import Javax.jws.soap.SOAPBinding.Style; 
Import Javax.xml.ws.Endpoint; 
@WebService (targetnamespace= "Http://loalhost:9999/helloworld") 
@SOAPBinding (STYLE=STYLE.RPC)//only supports RPC message style 
public class HelloWorld { 
//below @webmethod annotations, external disclosure method @WebMethod the publicly 
String SayHello () { 
return "HelloWorld"; 
} 
public static void Main (string[] args) { 
endpoint.publish ("Http://localhost:9999/helloworld", New HelloWorld ()); 
} 

2: The following is the WebService code published on jdk1.6.0_24:

Package com.itcast; 
Import Javax.jws.WebService; 
Import Javax.xml.ws.Endpoint; 
@WebService//Note Only this annotation is required, and the default SOAP message style is: DOCUMENT public 
class HelloWorld {public 
String SayHello () { return 
"HelloWorld"; 
} 
public static void Main (string[] args) { 
endpoint.publish ("Http://localhost:9999/helloworld", New HelloWorld ()); 
} 
}

First WebService Service

Package com.itcast; 
Import Javax.jws.WebService; 
Import Javax.xml.ws.Endpoint; 
/** 
* The first WebService service Application 
////////////through annotations, indicating that this publication is a WebService 
@WebService public 
class HelloWorld {public 
String SayHello () {return 
' Hello world '; 
} 
In the main method, use the Javax.xml.ws.Endpoint endpoint to publish an application public 
static void Main (string[] args) { 
endpoint.publish (" Http://127.0.0.1:9999/helloworld ", 
new HelloWorld ()); 
} 

Code Description: All Non-static public methods of the HelloWorld class are exposed externally.

Wsimport Tool Description:

wsimport is the JDK, which generates the tools for client-side calling code based on the WSDL document. Of course, the Java code is generated on the client, regardless of the language in which the server-side webservice is written. It doesn't matter what the server-side writes.

wsimport.exe is located in the Java_home\bin directory.

the commonly used parameters are:

-d< Directory >-The. class file will be generated. Default parameters.

-s< directory >-will generate. java files.

-p< generated new package name >-The generated class is placed under the specified package.

(wsdlurl)-HTTP://SERVER:PORT/SERVICE?WSDL, required parameters

Use:

1: You can check your current version number by Java–version. If the version is too low, you can install a high version of the JDK.

or copy someone else's installed JDK directory to your machine, such as the D:\jdk1.6.0_24 directory.

Because the previous environment variable has been set to the previous version of the JDK directory, that is, the java_home and path two environment variables.

You can reset the environment variable to: Java_home=d:\jdk1.6.0_24,path=%java_home%\bin,

After you reset the environment variables, reopen a doc (command line) window.

If you do not want to modify a previously configured environment variable, you can command the line window to enter the following command to make the jdk1.6.0_24 effective:

Set path = d:\jdk1.6.0_24\bin;%path% (Enter)

Then java–version to see if the JDK version number has changed.

2: Go to a relatively clean directory, I set up a new directory on D disk named: WS, and go to this directory.

3: Turn on your webservice.

4: Enter the following command:

Wsimport–s. http://127.0.0.1:9999/helloworld?wsdl

Parameter Description:-S refers to the source code file compiled, behind. (point) means to place the code in front of the record.

The last HTTP ... is the address that gets the WSDL specification.

5: At this point, the. java file and the. class file are generated. (both contain the original package name). Copy the code to your project. (Copy only Java files)

6: In the new project, a new class, (can be located under any package), the above generated code to call, see the next page ppt.

7:wsimport Other parameter descriptions, we often use the parameters for-d,-s,-p

The-d< directory > will generate a. class file.

Example: Wsimport–d. http://127.0.0.1:9999/helloworld?wsdl

The-s< directory > will generate. java files.

Example: Wsimport–s. http://127.0.0.1:9999/helloworld?wsdl

-p< Package Name > Modify the generated file (. java or. class to the specified package name)

Example: Wsimport-s. -P Com.beijing.itcast http://127.0.0.1:9999/helloworld?wsdl

For the-P argument, note the modification of the package name, which places all the generated classes under the package specified by-p. Presentation

It should be explained that when only the-p argument is used, it is also compiled into a. class file using-D. The –d parameter writes or does not write, it all is there, does not abandon.

Runmain.java's source code is as follows:

Package com.leaf; 
Import Com.itcast.HelloWorld; 
Import Com.itcast.HelloWorldService; 
/** 
* Call remote code by calling the generated class 
runmain {public 
static void Main (string[] args) { 
// Returns the calling interface 
HelloWorld HelloWorld = 
new HelloWorldService () from the Helloworldserice Gethelloworldport method. Gethelloworldport (); 
String str = Helloworld.sayhello (); Execute call 
System.err.println (str);//Return HelloWorld string 
} 

The difference between WebService and ordinary Web programs

1, WebService only uses the HTTP POST method transmission data, does not use get way;

1) The ContentType of Tttp Post is

(1) application/x-www-form-urlencoded

2) The ContentType of WebService is

(2) Text/xml soap1.1
(3) application/soap+xml–soap1.2

2, webservice from the data transmission format has been limited.

The data used by WebService is based on XML format. The current standard WebService uses SOAP protocols primarily in the data format. The SOAP protocol is actually a text protocol based on XML Coding specification.

The difference between WebService and Web server:

We can think of webservice as an application on a Web server; Conversely, a Web server is a container that is necessary for the WebService runtime. This is their difference and connection.

Characteristics of WebService:

1, webservice through the HTTP POST method to accept customer requests

2, WebService and the client generally use the SOAP protocol to transfer XML data.

3, it is designed for cross-platform or cross language.

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.