不需要Web應用服務器Java實現WebServices
近日來,在社區內瀏覽了一部分關于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>
以上兩個例子我們可以看出,Java來實現WebServices的時候Web應用服務器不是必須的,希望各位同學能夠因地制宜好好的利用WebServices所提供的各種優勢,我們要不僅會做,還要善于做!
原文鏈接:http://hwak.iteye.com/blog/1222660
編輯推薦: