当前位置: 代码迷 >> Web前端 >> Java兑现WebServices不需要Web 应用服务器
  详细解决方案

Java兑现WebServices不需要Web 应用服务器

热度:818   发布时间:2012-09-07 10:38:15.0
Java实现WebServices不需要Web 应用服务器

Java实现WebServices不需要Web 应用服务器

?

?

????? 近日来,在社区内浏览了一部分关于Java来实现WebServives的帖子,发现其中90%以上都有这样一步操作

?

首先在进行webservice 一定要下载Axis安装包。。。
将某某文件配置到%TOMCAT_HOME%\webapps\axis\WEB-INF。。。
然后在web.xml 加上。。。?

??????? 实际上这种做法本身没有错,但是却扼杀了WebServices的诸多优点。那么让我们从头了解一下什么是WebServices吧。

??????

百度百科中写道:
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。它是一种构建应用程序的普遍模型,可以在任何支持网络通信的操作系统中实施运行;它是一种新的web

应用程序分支,是自包含、自描述、模块化的应用,可以发布、定位、通过web调用。

Web Service是一个应用组件,它逻辑性的为其他应用程序提供数据与服务.各应用程序通过网络协议和规定的一些标准数据格式(Http,XML,Soap)来访问Web Service,通过Web Service内部执行得到所需结果.Web Service可以执行从简单的请求到复杂商务处理的任何功能。一旦部署以后,其他Web Service应用程序可以发现并调用它部署的服务。



在构建和使用Web Service时,主要用到以下几个关键的技术和规则:

  1.XML:描述数据的标准方法.

  2.SOAP:表示信息交换的协议.

  3.WSDL:Web服务描述语言.

  4.UDDI(Universal Description, Discovery and Integration):通用描述、发现与集成,它是一种独立于平台的,基于XML语言的用于在互联网上描述商务的协议。 实际上,WebService的主要目标是跨平台的可互操作性。为了达到这一目标,WebService完全基于XML(可扩展标记语言)、XSD(XMLSchema)等独立于平台、独立于软件供应商的标准,是创建可互操作的、分布式应用程序的新平台。

长项一:跨防火墙的通信

长项二:应用程序集成

长项三:B2B的集成

?

??????回到标题我所说的,WebServices真的一定必须要什么Jar包吗?需要插件么?实际上webservice实现有多种方式比如最常用的有axis框架,xfire框架,通过该框架可以发布wsdl接口,也可以实现webservice客户端,目前eclipse都有集成的插件,可以根据wsdl文件生成webservice客户端调用接口,但是这样部署的时候必须依赖框架的jar包,有时候可能因为环境等等原因,我们仅仅需要wsdl中的某一个接口,这时候可以通过http接口或socket接口直接发生xml数据,来调用服务端webservice服务,其实webservice底层还是发送xml数据,只是框架封装了对xml数据进行序列化与反序列化操作,下面以两个简单的例子说明http方式和socket方式。

第一个例子:http实现webservice接口调用例子:

import java.io.BufferedReader;       
import java.io.IOException;       
import java.io.InputStreamReader;       
import java.io.OutputStreamWriter;       
import java.io.UnsupportedEncodingException;       
import java.net.MalformedURLException;       
import java.net.URL;       
import java.net.URLConnection;       
      
public class HttpPostTest {       
    void testPost(String urlStr) {       
        try {       
            URL url = new URL(urlStr);       
            URLConnection con = url.openConnection();       
            con.setDoOutput(true);       
            con.setRequestProperty("Pragma:", "no-cache");       
            con.setRequestProperty("Cache-Control", "no-cache");       
            con.setRequestProperty("Content-Type", "text/xml");       
                   
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());           
            String xmlInfo = getXmlInfo();       
            out.write(new String(xmlInfo));       
            out.flush();       
            out.close();       
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));       
            String line = "";       
            StringBuffer buf = new StringBuffer();       
            for (line = br.readLine(); line != null; line = br.readLine()) {       
                buf.append(new String(line.getBytes(),"UTF-8"));       
            }       
            System.out.println(buf.toString());       
        } catch (MalformedURLException e) {       
            e.printStackTrace();       
        } catch (IOException e) {       
            e.printStackTrace();       
        }       
    }       
      
    private String getXmlInfo() {       
        // 通过wsdl文件可以查看接口xml格式数据,构造调用接口xml数据       
        String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"      
                    + "<SOAP-ENV:Body>"      
                    +    "<m:getItemDetailSingle xmlns:m=\"http:xxxxxxxxxxxxxxxxxx/\">"      
                    +        "<itemMo>"      
                    +            "<category>政务域名</category>"      
                    +            "<city>北京西坝河北里</city>"      
                    +            "<flag>3</flag>"      
                    +            "<itemId>10</itemId>"      
                    +            "<itemIndex>22</itemIndex>"      
                    +            "<keyword>朝阳区</keyword>"      
                    +            "<mobile>139-0111-1111</mobile>"      
                    +            "<password>iteyePl</password>"      
                    +            "<userName>hwak</userName>"      
                    +        "</itemMo>"      
                    +    "</m:getItemDetailSingle>"      
                    + "</SOAP-ENV:Body>"      
                    + "</SOAP-ENV:Envelope>";       
        return xml;       
    }       
      
    public static void main(String[] args) throws UnsupportedEncodingException {       
        String url = "http://localhost:9999/dataService/services/Job";       
        new HttpPostTest().testPost(url);       
    }       
}</PRE>  

?

?

第二个例子:socke方式实现例子:

?

import java.io.IOException;       
import java.io.InputStream;       
import java.io.InputStreamReader;       
import java.io.OutputStream;       
import java.net.Socket;       
import java.net.UnknownHostException;       
      
      
public class WebServiceClient {       
      
    /**     
     * @param args     
     * @throws IOException      
     * @throws UnknownHostException      
     */      
    public static void main(String[] args) throws UnknownHostException, IOException {       
        Socket socket = new Socket("localhost",9003);          
        OutputStream os = socket.getOutputStream();          
        InputStream is = socket.getInputStream();          
        //System.out.println(socket.isConnected());       
        String httpSend = "POST /dataService/services/Job HTTP/1.1\r\n"         
                        + "Content-Type:text/xml\r\n"         
                        + "Host:localhost:9003\r\n"         
                        + "Content-Length:1024\r\n"         
                        + "\r\n"         
                        + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"      
                        + "<SOAP-ENV:Body>"      
                        +    "<m:getItemDetailSingle xmlns:m=\"http://localhost/\">"      
                        +        "<itemMo>"      
                        +            "<category>工厂类</category>"      
                        +            "<city>北京</city>"      
                        +            "<flag>1</flag>"      
                        +            "<itemId>0</itemId>"      
                        +            "<itemIndex>1</itemIndex>"      
                        +            "<keyword>String</keyword>"      
                        +            "<mobile>2147483647</mobile>"      
                        +            "<password>123456</password>"      
                        +            "<userName>sohu</userName>"      
                        +        "</itemMo>"      
                        +    "</m:getItemDetailSingle>"      
                        + "</SOAP-ENV:Body>"      
                        + "</SOAP-ENV:Envelope>";       
        os.write(httpSend.getBytes());          
        os.flush();          
         
        InputStreamReader ireader = new InputStreamReader(is);          
        java.io.BufferedReader breader = new java.io.BufferedReader(ireader);          
                  
        String responseLine = "";          
                  
        while((responseLine = breader.readLine()) != null)          
        {          
            System.out.println(new String(responseLine.getBytes(),"UTF-8"));          
        }          
                   
        System.out.println("");          
}}

?

????? 以上两个例子我们可以看出,Java来实现WebServices的时候Web应用服务器不是必须的,希望各位同学能够因地制宜好好的利用WebServices所提供的各种优势,我们要不仅会做,还要善于做!

1 楼 snkcxy 2012-04-17  
我想请教一下 WebServices 相对于socket和http的方式提供网络服务,有什么优势吗?
  相关解决方案