release of Java6 WebServiceWebService Service Release is often chaotic, Axis2 release form and Xfire release way is very different, and JAVA6 Web Service Publishing and Axis2, Xfire Web Service publish way also have a difference, they have no experience to learn from. Therefore, it is necessary to delve into the way JAVA6 Web services are published. As you can see from the previous two articles, JAVA6 provides direct support for the API level of WebService release, which requires a single line of code. However, as a service, you need to start, you can not start each time to run a Main method to start it. In practice, the release of Web services is often associated with the launch of web containers, and in Java6, we cannot directly publish as Axis2 and Xfire, but can be used in a flexible way, through the servlet to bind to the container, when the container starts to publish the service. The following is an example of Tomcat, which implements the publication of the JAVA6 Web service.
1, Development WebService service
PackageLavasoft.server;ImportJavax.jws.WebService;/*** JAVA6 Developed WebService server * *@authorleizhimin 2009-11-16 10:24:13*/@WebService Public classJava6ws {/*** Business Methods in Web Services * *@returnA String*/ Publicstring dosomething (string username) {returnUsername + "is doing something!"; } }
2, development and release services servlet, in order to universality, it is best to choose Genericservlet to inherit
PackageLavasoft.servlet;ImportLavasoft.server.Java6WS;Importjavax.servlet.*; ImportJavax.xml.ws.Endpoint;Importjava.io.IOException;/*** Release JAVA6 WebService servlet, * *@authorleizhimin 2009-11-16 13:52:49*/ Public classWsservletextendsGenericservlet {@Override Public voidInit (ServletConfig servletconfig)throwsservletexception {Super. Init (ServletConfig); System.out.println ("Ready to start WebService service: Http://192.168.14.117:8888/java6ws/Java6WS"); //Publish a WebServiceEndpoint.publish ("Http://192.168.14.117:8888/java6ws/Java6WS",NewJava6ws ()); System.out.println ("WebService Service started successfully: Http://192.168.14.117:8888/java6ws/Java6WS"); } Public voidService (ServletRequest servletrequest, Servletresponse servletresponse)throwsservletexception, IOException {System.out.println ("This servlet does not process any business logic, just Yonglai publish a Web service: Http://192.168.14.117:8888/java6ws/Java6WS"); } }
The code for the Service release is written in the servlet init () method, which automatically executes the method in Init () when the servlet is loaded. In order to achieve the purpose of publishing the service, since this servlet does not handle any business, only a hint statement is written in the service.
3. Configure Web. XML
<?XML version= "1.0" encoding= "UTF-8"?> <Web-appxmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version= "2.5"> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list> <servlet> <Servlet-name>Wsservlet</Servlet-name> <Servlet-class>Lavasoft.servlet.WSServlet</Servlet-class> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Wsservlet</Servlet-name> <Url-pattern>/servlet/wsservlet</Url-pattern> </servlet-mapping> </Web-app>
Note that the above servlet has one more line than the generic servlet configuration: <load-on-startup>1</load-on-startup> The purpose of this writing is that the servlet can be loaded with an easy boot at the first time. So that the service can be released as early as possible, otherwise it will only be loaded into the container the first time the servlet is requested.
4. Deploy, start, and testStart Tocmat6, enter in the browser: http://localhost:8080/java6ws/click on the hyperlink, you can get to the WSDL as follows:
As you can see, the service was successfully launched with Tomcat launch.
5. Summary1), Java6 the overall idea of the service release is through the JAVA6 API call to achieve the purpose of the release, as of now, have seen two publishing methods, one is to call API:Endpoint.publish directly ("http://192.168.14.117:8888/java6ws/java6ws", New Java6ws ()); The other is, in this case, the servlet way. 2), Endpoint.publish is a background thread that, once published, waits for a request to process the Web service and exits automatically only when the main method exits or when the container is closed. 3), in fact, in addition to this way, can also integrate spring to achieve the release of services, which is integrated with the spring IOC container. No matter what, the reason is one, call the Endpoint.publish () method to implement the service release. 4), when publishing the service, should avoid the use of the Tomcat container port, one side of the conflict, in this article, Tomcat uses the port is 8080, and the Web service publishing uses the port is 8888.
Release of Java6 WebService