日期:2014-05-16  浏览次数:20299 次

as3.0和jsp值传递(通过xml方式)(转)
今天看到一篇不错的as和jsp通信的例子,转了.
as3:
var request:URLRequest=new URLRequest("目标页面");
var loader:URLLoader=new URLLoader();
loader.load(request)
request.method=URLReqeustMethod.GET;

jsp:display.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%
   out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
   out.println("<root>");
   String userName="";
   String pwd="";
   String url="jdbc:mysql://localhost:3306/pic?        user=userName&password=pwd&useUnicode=true&charsetEncoding=utf-8";
   String sql="select * from pic_upload";
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   Connection conn=DriverManager.getConnection(url);
   Statement stm=conn.createStatement();
   ResultSet rs=stm.executeQuery(sql);
   while(rs.next()){
      String pic_count=rs.getString("id");
      String pic_path=rs.getString("pic_path");
      String pic_name=rs.getString("pic_name");
      out.println("<node>");
      out.println("<id>"+pic_count+"</id>");
      out.println("<name>"+pic_name+"</name>");
      out.println("<path>"+pic_path+"</path>");
      out.print("</node>"+"\n");
   }
   out.println("</root>");
%>

as3
private function jspLoaded(e:Event):void {
   myXML=XML(loader.data);//trace(loader.data)就可以看到jsp输出的页面内容,必须要转化成XML类型
   var nodes:XMLList=myXML.children();//这句代码相当重要用children()方法进行遍历。目的是获得所有的节点(node)
   var i:int=nodes.length()-1;//获得节点的个数,简单
   var n:String=myXML.node[i].name;//输出最后一个节点下name属性的值,你可以用for循环进行遍历输出
   //下面是new一个tileList组建进行测试的
   var dp:DataProvider=new DataProvider;
   //dp.addItemAt({Label:"images1",source:"http://localhost:888/flashup/upload/"+n},i);
   dp.addItem({Label:"the Last one",source:"http://localhost:888/flashup/upload/"+n});
   var tileList:TileList=new TileList;
   tileList.dataProvider=dp;
   tileList.columnWidth=500;
   tileList.rowHeight=350;
   tileList.columnCount=3;
   tileList.rowCount=3;
   addChild(tileList);
   //trace(myXML.node.name);
  }