.......VC程序员,无任何web开发经验,赶鸭子上架只能学了目前只想实现一个webservice,读取数据库然后将检索结果构造成xml形式return,客户端自己再解析出内容,很简单的入门级别类,JDBC操作无问题,希望有例子的朋友指点下代码,谢了

解决方案 »

  1.   

    如果不涉及到SOAP,用Servlet/JSP直接就可以做了。
      

  2.   


    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.xml.ws.Endpoint;@WebService(targetNamespace = "http://jdk.study.hermit.org/client")
    @SOAPBinding(style = SOAPBinding.Style.RPC)
    public class Hello
    {
    @WebMethod
    public String sayHello(String name)
    {
    return "hello:" + name;
    }

    public static void main(String[] args)
    {
    Endpoint.publish("http://localhost:80/hello", new Hello());
    }
    }
    运行这个程序,然后访问你本机:http://localhost:80/hello?wsdl,可以看到如下页面<?xml version="1.0" encoding="UTF-8"?><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://jdk.study.hermit.org/client" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://jdk.study.hermit.org/client" name="HelloService">
      <types></types>
      <message name="sayHello">
        <part name="arg0" type="xsd:string"></part>
      </message>
      <message name="sayHelloResponse">
        <part name="return" type="xsd:string"></part>
      </message>
      <portType name="Hello">
        <operation name="sayHello" parameterOrder="arg0">
          <input message="tns:sayHello"></input>
          <output message="tns:sayHelloResponse"></output>
        </operation>
      </portType>
      <binding name="HelloPortBinding" type="tns:Hello">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
        <operation name="sayHello">
          <soap:operation soapAction=""></soap:operation>
          <input>
            <soap:body use="literal" namespace="http://jdk.study.hermit.org/client"></soap:body>
          </input>
          <output>
            <soap:body use="literal" namespace="http://jdk.study.hermit.org/client"></soap:body>
          </output>
        </operation>
      </binding>
      <service name="HelloService">
        <port name="HelloPort" binding="tns:HelloPortBinding">
          <soap:address location="http://localhost/hello"></soap:address>
        </port>
      </service>
    </definitions>
      

  3.   

    刚才只是如何使用WS,关于XML解析,自己去查DOM4J文档或去网上搜.大把大把的.
      

  4.   


    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.xml.ws.Endpoint;@WebService(targetNamespace = "http://jdk.study.hermit.org/client")
    @SOAPBinding(style = SOAPBinding.Style.RPC)
    public class Hello
    {
        @WebMethod
        public String sayHello(String name)
        {
            return "hello:" + name;
        }
        
        public static void main(String[] args)
        {
            Endpoint.publish("http://localhost:80/hello", new Hello());
        }
    }同意3 楼