先贴出来一个数据通信的例子:
参考例子:
首先创建flash端:在主场景第一层第一帧上放入所需要控件一个两个输入文本框一个输入字符串一个输入数字,一个提交按钮,一个现实服务器返回的数据的动态文本框
再新建一层,再第一帧写入以下代码:
this.send_btn.addEventListener(MouseEvent.CLICK,fasong);
function fasong(event:MouseEvent):void {
var variables:URLVariables = new URLVariables();
variables.usename=this.shuru.text;
variables.word=this.shuru2.text;
var url:String="http://localhost:8080/demo3/sentVar.jsp";
var r:URLRequest=new URLRequest(url);
r.data=variables;
r.method=URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.dataFormat=URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE,wancheng);
try {
loader.load(r);
} catch (error:Error) {
trace("加载错误");
}
}
function wancheng(event:Event):void {
var my:URLLoader=URLLoader(event.target);
trace(my.data);
trace(my.data.a);
trace(my.data.b);
this.jieshou.text=my.data.a;
this.jieshou2.text=my.data.b;
}
然后在/demo3/sentVar.jsp中写入:
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'jsj.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script lanaguage="javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function startRequest(){
createXMLHttpRequest();
xmlHttp.open("GET","sentVar.jsp",true);
xmlHttp.onReadystatechange=function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
alter("服务器返回:" + xmlHttp.responseText);
}
}
window.onload=function(){
application.setAttribute("user","session!");
}
</script>
</head>
<body>
<%
System.out.println("开始获取数据");
String name = request.getParameter("usename");
String password = request.getParameter("word");
Integer pass=0;
if(password!=null){
pass = Integer.parseInt(password);
pass=pass+4;
}
if(name!=null){
application.setAttribute("user",name);
application.setAttribute("word",String.valueOf(pass));
}
System.out.println("获取数据成功"+name);
out.clear();
out.println("a="+application.getAttribute("user")+"Success!"+"b="+application.getAttribute("word"));
System.out.println("发送数据成功");
%>
</body>
</html>
这样启动tomcat就可以完成数据通信。
还有一种方法就是利用xmlsocket,但是现在还没完全弄明白,因为如果用这个的话,要是用tomcat作为服务端的话会出现沙箱安全问题。先贴出来写的代码,后续工作继续中。。。
package{
import flash.display.Sprite;
import flash.events.*;
import flash.net.XMLSocket;
public class send extends Sprite{
private var socket:XMLSocket;
var xmldata:XML=
<login>
<user>
<name>jack</name>
<password>1111</password>
</user>
</login>;
public function send(){
socket = new XMLSocket();
socket.addEventListener(Event.CONNECT,onConnect);
socket.connect("localhost",8080);
socket.send(xmldata);
}
private function onConnect(event:Event): void{
trace("The socket is now connector...");
}
}
}