Next, in the previous article, here is an example of a client-side handler.
First, paste the whole WebService server code
WebService:
Import javax. JWS. handlerchain; <br/> Import javax. JWS. webmethod; <br/> Import javax. JWS. webService; </P> <p> @ WebService <br/> @ handlerchain (file = "handlers. XML ") <br/> public class hellows {</P> <p> @ webmethod <br/> Public String sayhello (string name) {<br/> return "hello" + name + ". "; <br/>}< br/>}
Handler:
Import Java. util. set; </P> <p> Import javax. XML. namespace. QNAME; <br/> Import javax. XML. WS. handler. messagecontext; <br/> Import javax. XML. WS. handler. soap. soaphandler; <br/> Import javax. XML. WS. handler. soap. soapmessagecontext; </P> <p> public class hellohandler implements soaphandler <soapmessagecontext >{</P> <p> @ override <br/> Public Boolean handlemessage (soapmessagecontext context) {<br/> system. out. println (context. get (messagecontext. wsdl_service ). tostring (); <br/> return true; <br/>}</P> <p> @ override <br/> Public Boolean handlefault (soapmessagecontext context) {<br/> // todo auto-generated method stub <br/> return true; <br/>}</P> <p> @ override <br/> Public void close (messagecontext context) {<br/> // todo auto-generated method stub </P> <p >}</P> <p> @ override <br/> Public set <QNAME> getheaders () <br/>{< br/> // todo auto-generated method stub <br/> return NULL; <br/>}</P> <p >}< br/>
Here we use j2se to publish WebService. The main function is:
Import javax. XML. WS. endpoint; </P> <p> public class servermain {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) <br/> {<br/> endpoint. publish ("http: // localhost: 8000/hellows3/hellowsservice", new hellows (); </P> <p >}</P> <p>}
Run the main function and publish WebService. The WSDL address is http: // localhost: 8000/hellows3/hellowsservice? WSDL
Generate client code
Use the wsimport command to generate client code
Command :... /Workspace/hellows3client/bin> wsimport-keep http: // localhost: 8000/hellows3/hellowsservice? WSDL-P com. ws. Client
Here, keep indicates saving the source file, and-P indicates saving the code to the specified package.
Create a main client function under com. ws. Client, and insert handler in the WebService proxy object of the client. To facilitate the use of the same handler class above, the client code is as follows:
Import Java. util. list; </P> <p> Import javax. XML. WS. binding; <br/> Import javax. XML. WS. bindingprovider; <br/> Import javax. XML. WS. handler. handler; </P> <p> Import COM. WS. handler. hellohandler; </P> <p> public class clientmain {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) <br/>{< br/> hellowsservice HS = new hellowsservice (); </P> <p> hellows Hello = HS. gethellowsport (); </P> <p> binding = (bindingprovider) Hello ). getbinding (); <br/> List <pandler> handlerchain = binding. gethandlerchain (); <br/> handlerchain. add (New hellohandler (); <br/> binding. sethandlerchain (handlerchain); </P> <p> system. out. println (hello. sayhello ("xxxxxxx"); </P> <p >}</P> <p>}
The purpose of the code is to add a handler in the handlerchain of the bindingprovider object.
To keep the previous WebService running on the server side and then run the client, you can print the name of the called WebService on both processes in the console.
Server:
2011-5-4 18:12:08 com. Sun. xml. Internal. ws. model. runtimemodeler getrequestwrapperclass
Information: dynamically creating request Wrapper class com. ws. Service. jaxws. sayhello
2011-5-4 18:12:08 com. Sun. xml. Internal. ws. model. runtimemodeler getresponsewrapperclass
Information: dynamically creating response wrapper Bean class com. ws. Service. jaxws. sayhelloresponse
{Http://service.ws.com/?hellowsservice
{Http://service.ws.com/?hellowsservice
Client:
{Http://service.ws.com/?hellowsservice
{Http://service.ws.com/?hellowsservice
Hello XXXXXXX. s