Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JAX-WS of JDK1.6 to write WebService

Shulou Source: shulou.com Published: 2022-06-02 08:10:26 11月16日 Update

This article will explain in detail how to use JDK1.6 's JAX-WS to write WebService. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

I. the concept of Web services

Web services is the interaction between the client and the server through the HTTP protocol of the World wide Web.

2. JAX-WS implements simple Web services.

2.1 build a Web application named HelloServer as a Webservice client

2.2 create a new class under the HelloServer application:

Package helloservice.endpoint; import javax.jws.WebMethod; import javax.jws.WebService; @ WebService public class Hello {private String message = new String ("Hello,"); public void Hello () {} @ WebMethod public String sayHello (String name) {return message + name + ".";}}

2.3 publish the HelloServer application under weblogic, the application name is WebRoot.

2.4 Open http://localhost:7001/WebRoot/HelloService?wsdl in IE

If you can view the content of wsdl, it means that the release is successful. For example:

2.5 run wsimport

Wsimport is unique to JDK1.6, under [JAVA_HOME] / bin.

2.5.1 under the E:\ Program Files\ PowerCmd > directory, create a new folder generate.

2.5.2 run the following command:

Wsimport-s generate http://localhost:7001/WebRoot/HelloService?wsdl

If you return

Parsing WSDL...generating code...

It indicates that the operation is successful.

2.5.3 looking at the generate directory, you can see that the JAVA file is generated, and there are class files in the directory at the same level as generate. (the JAVA file generated here is needed by the client.)

The generated HelloService.java is as follows:

Package helloservice.endpoint; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceFeature; / * * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.1 in JDK 6 * Generated source version: 2.1 * * / @ WebServiceClient (name = "HelloService", targetNamespace = "http://endpoint.helloservice/", wsdlLocation =" http://localhost:7001/WebRoot/HelloService?wsdl") public class HelloService extends Service {private final static URL HELLOSERVICE_WSDL_LOCATION; static {URL url = null Try {url = new URL ("http://localhost:7001/WebRoot/HelloService?wsdl");} catch (MalformedURLException e) {e.printStackTrace ();} HELLOSERVICE_WSDL_LOCATION = url;} public HelloService (URL wsdlLocation, QName serviceName) {super (wsdlLocation, serviceName) } public HelloService () {super (HELLOSERVICE_WSDL_LOCATION, new QName ("http://endpoint.helloservice/"," HelloService ")) } / * * @ return * returns Hello * / @ WebEndpoint (name = "HelloPort") public Hello getHelloPort () {return (Hello) super.getPort (new QName ("http://endpoint.helloservice/"," HelloPort "), Hello.class) } / * @ param features * A list of {@ link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the features parameter will have their default values. * @ return * returns Hello * / @ WebEndpoint (name = "HelloPort") public Hello getHelloPort (WebServiceFeature... Features) {return (Hello) super.getPort (new QName ("http://endpoint.helloservice/"," HelloPort "), Hello.class, features);}}

Build a Web application named HelloClient as a WebService client.

2.7 copy the JAVA file generated by 3.5.3 to the src of HelloClient.

Create a new HelloServlet file, as follows:

Package webclient; import helloservice.endpoint.HelloService; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.ws.WebServiceRef WebServlet (name = "HelloServlet", urlPatterns = {"/ HelloServlet"}) public class HelloServlet extends HttpServlet {@ WebServiceRef (wsdlLocation = "http://localhost:7001/WebRoot/HelloService?wsdl") private HelloService service; / * Constructor of the object. * / public HelloServlet () {super ();} / * * Destruction of the servlet.

* / public void destroy () {super.destroy (); / / Just puts "destroy" string in log / / Put your code here} / * The doGet method of the servlet.

* * This method is called when a form has its tag value method equals to get. * * @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred * / public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {processRequest (request, response);} / * The doPost method of the servlet.

* * This method is called when a form has its tag value method equals to post. * * @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred * / public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {processRequest (request, response);} / * Initialization of the servlet.

* * @ throws ServletException if an error occurs * / public void init () throws ServletException {/ / Put your code here} / * * Processes requests for both HTTP GET and POST * methods. * * @ param request * servlet request * @ param response * servlet response * @ throws ServletException * if a servlet-specific error occurs * @ throws IOException * if an O error occurs * / protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException IOException {response.setContentType ("text/html Charset=UTF-8 "); PrintWriter out = response.getWriter (); try {out.println ("); out.println ("); out.println (" Servlet HelloServlet "); out.println ("); out.println (") Out.println ("Servlet HelloServlet at" + request.getContextPath () + "); out.println ("

"+ sayHello (" world ") +"

"); out.println ("); out.println (");} finally {out.close ();}} / / doGet and doPost methods, which call processRequest, and / / getServletInfo method private String sayHello (java.lang.String arg0) {helloservice.endpoint.Hello port = service.getHelloPort (); return port.sayHello (arg0);}}

2.9 configure the Web.xml of HelloClient by adding the following code:

HelloServlet webclient.HelloServlet HelloServlet / servlet/HelloServlet

2.10 release the HelloClient application.

2.11 enter http://localhost:7111/servlet/HelloServlet in IE

The content of the page is as follows: the WebService call is successful!

This is the end of this article on "how to write WebService using JDK1.6 's JAX-WS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Files applications clients clients builds success content directories articles runs more good practical World wide Web code siblings commands folders articles concepts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info Apple vpn macOS