Flash and Jsp/servlet

Source: Internet
Author: User

In AS2, you typically use the Loadvars class to exchange data with a Web server:

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);

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

In AS3, the equivalent of:

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): is actually sending a request to the server:

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

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

Where communicate_flash/index.jsp is a Web application that we deploy under Tomcat WebApps:

index.jsp files are as follows:

<%@ 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"));
%>

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

When the data returned from the JSP page is received, the handleloadsuccessful () function is called: The data is obtained by Evt.target.data:

result1=天天one,result2=快乐two

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

Finally, don't forget that JSP is the servlet, so you can communicate with the JSP and the servlet. Of course, with PHP, ASP communication methods are the same. The above is flash and background communication The simplest and most direct method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.