Flash與jsp/servlet

來源:互聯網
上載者:User

在AS2中通常使用LoadVars類與Web伺服器交換資料:

var msg:LoadVars = new LoadVars();
var msgSent:LoadVars = new LoadVars();

msg.var1 = "one";
msg.var2 = "two";

msgSent.onLoad = function(success:Boolean):Void {
    if (success) {
        trace("Message sent.");
    }
    else {
        trace("Message failed.");
    }
};
msg.sendAndLoad ("http://127.0.0.1:8080/communicate_flash/index.jsp", msgSent);

==================================================================== ===============

在 AS3 中,等同於:

var scriptRequest:URLRequest = new URLRequest ("http://127.0.0.1:8080/communicate_flash/index.jsp");
var scriptLoader:URLLoader = new URLLoader();
var scriptVars:URLVariables = new URLVariables();

scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);

scriptVars.var1 = "one";
scriptVars.var2 = "two";

scriptRequest.method = URLRequestMethod.POST;
scriptRequest.data = scriptVars;

scriptLoader.load(scriptRequest);

function handleLoadSuccessful(evt:Event):void {
    trace("Message sent.");
    trace("DataReceived:" + evt.target.data);
}

function handleLoadError(evt:IOErrorEvent):void {
    trace("Message failed.");
}

==================================================================== ===============

scriptLoader.load(scriptRequest): 實際上就是向伺服器發送了這樣一個請 求:

http://127.0.0.1:8080/communicate_flash/index.jsp? var1=one&var2=two

==================================================================== ===============

其中 communicate_flash/index.jsp 是我們在 Tomcat 中 webapps 下部署的 一個 web 應用程式:

index.jsp 檔案如下:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
    System.out.println("----- connected -----");

    // 在和 flash 通訊時,請保證字元集為 UTF-8,否則傳輸中文會產生亂 碼
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");

    out.print("result1=天天"+request.getParameter ("var1")+",");
    out.print("result2=快樂"+request.getParameter ("var2"));
%>

==================================================================== ===============

收到jsp頁面返回的資料後,會調用handleLoadSuccessful()函數:通過 evt.target.data得到資料:

result1=天天one,result2=快樂two

==================================================================== ===============

最後別忘了 jsp 就是servlet,因此能和 jsp 通訊也就能和 servlet 通訊。 當然與 php, asp 通訊方法也都是這樣的。以上就是flash與後台通訊最簡單、最 直接的方法。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.