Developing WebService in Java 6 -- quick practice

Source: Internet
Author: User

Developing WebService in Java 6 ---- quick practices

1. Create a web project wstest

Use Eclipse and right-click to create a web project.

2. Create a package

Com. test. ws

3. Create a Server File com. test. ws. Hello. java


Package com. test. ws;
Import javax. jws. WebService;
Import javax. jws. soap. SOAPBinding;
Import javax. xml. ws. Endpoint;
Import javax. jws. soap. SOAPBinding. Style;

@ WebService
@ SOAPBinding (style = Style. RPC)
Public class Hello {
/**
* Business methods in Web Services
* @ Return a string
*/
Public String sayHi (String name ){
Return "hi," + name;
}

Public static void main (String [] args ){
// Publish a WebService
Endpoint. publish ("http: // localhost: 81/hello", new Hello ());
}
}
Note: Due to the JDK version, @ SOAPBinding (style = Style. RPC) must be added to the version I use)
Otherwise, the error "Wrapper class com. test. ws. jaxws. SayHi is not found. Have you run APT to generate them? ".

4. Test the server program

(1) run Hello. java
(2) Enter http: // localhost: 81/hello in the browser? The corresponding description is displayed in the wsdl.

5. Create a client

(1) Method 1: Automatic Creation



(1) enter the doscommand line status and enter the src directory of the wstest Project


(2) execute the following command:
D:/workspace/wstest/src> wsimport-p com. test. ws. client-keep http: // localhost: 81/hello? Wsdl
Note: you must first run Hello. java to enable the Service first. Otherwise, an error will be reported.


(3) Go to Eclipse and press F5 to refresh the wstest project. com. test. ws has an additional client Directory, and four files are automatically created under the client Directory: Hello. class, Hello. java, HelloService. class, HelloService. java


(4) create a client test class com. test. ws. client. TestClient. java
Package com. test. ws. client;

Public class TestClient {
Public static void main (String args []) {
Hello hello = new HelloService (). getHelloPort ();
String msg = hello. sayHi ("tom ");
System. out. println (msg );
}
}


(5) Running TestClient. java will output the following results:
Hi, tom


(2) Method 2: manually create a client (concise and easy to understand)


(1) Create the interface com. test. ws. client. HelloInte. java corresponding to the server

Package com. test. ws. client;



Import javax. jws. WebMethod;


Import javax. jws. WebParam;


Import javax. jws. WebResult;


Import javax. jws. WebService;


Import javax. jws. soap. SOAPBinding;



@ WebService (name = "Hello", targetNamespace = "http://ws.test.com /")


@ SOAPBinding (style = SOAPBinding. Style. RPC)


Public interface HelloInte {



@ WebMethod


@ WebResult (partName = "return ")


Public String sayHi (


@ WebParam (name = "arg0", partName = "arg0 ")


String arg0 );


}


(2) create a client test class com. test. ws. client. TestClient. java

Package com. test. ws. client;

Import javax. xml. namespace. QName;
Import javax. xml. ws. Service;
Import java.net. URL;

Public class TestClient {
Public static void main (String [] args) throws Exception {
URL url = new URL ("http: // localhost: 81/hello? Wsdl ");

// The first parameter is the service URI.
// The second parameter is the service name published in the WSDL.
QName qname = new QName ("http://ws.test.com/", "HelloService ");

// Create a service
Service service = Service. create (url, qname );

// Extract the endpoint interface and the service "port ".
HelloInte h = service. getPort (HelloInte. class );

System. out. println (h. sayHi ("michael "));
System. out. println (h. sayHi ("tom "));
}
}


(3) Run
TestClient, the following result is displayed:

Hi, michael
Hi, tom


Supplement: in order to verify the correlation between the server and the client, an error is reported when TestClient. java is executed again after Hello. java is stopped. It indicates that client access is related to the server.

6. WEB deployment

(1) create COM. Test. ws. Client. servlet. pubservlet. Java
Package com. Test. ws. servlet;

Import java. Io. ioexception;
Import javax. servlet. genericservlet;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;
Import javax. xml. ws. endpoint;
Import com. Test. ws. Hello;

Public class pubservlet extends genericservlet {
Public pubservlet (){
Super ();
}
Public void destroy (){
Super. destroy ();
}
@ Override
Public void init () throws ServletException {
Super. init ();
System. out. println ("start run webservice ...");
Endpoint. publish ("http: // localhost: 81/hello", new Hello ());

System. out. println ("Webservice is running ...");
}
@ Override
Public void service (ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
}
}
(2) modify web. xml
<Servlet>
<Servlet-name> PubServlet </servlet-name>
<Servlet-class> com. test. ws. servlet. PubServlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

<Servlet-mapping>
<Servlet-name> PubServlet </servlet-name>
<URL-pattern>/servlet/pubservlet </url-pattern>
</Servlet-mapping>

Note: To enable the Automatic startup of pubservlet, Set Load-on-startup to 1.
(3) After the release is completed, start the Web Service
(4) Run testclient. Java to test

7. Summary


(1) Whether directly running through the main () method or publishing through servlet, the endpoint. Publish () method is actually being executed.
(2) When publishing a web application, the port used by WebService must be separated from the port used by the Web container.
(3) compared with xfire, this method does not need to be added to a third-party package, making development easier. However, I personally prefer xfire. At present, the implementation of this client makes me feel uncomfortable. In addition, the release of xfire in Web containers is not convenient.

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.