flex与servlet交互

首先,我们在eclipse中创建一个web project 取名为simple

在这个project中我们创建一个servlet:代码如下:

package com.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<rss>");
out.println("<xml>");
out.println("<pubDate>pubDate11111</pubDate>");
out.println("<title>title111</title>");
out.println("<description>description111</description>");
out.println("</xml>");
out.println("<xml>");
out.println("<pubDate>pubDate222</pubDate>");
out.println("<title>title22</title>");
out.println("<description>description122211</description>");
out.println("</xml>");
out.println("</rss>");
}

}

这个servlet的访问地址为:http://localhost:8080/simple/test,
现在我们在flex buider3中创建一个flex project,选择web root为tomcat/webapps下面, 取名test.
新建一个connect.mxml文件 代码如下:
[code]
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="feedRequest.send()">
<mx:HTTPService id="feedRequest" url="http://localhost:8080/simple/test"
useProxy="false">

</mx:HTTPService>
<mx:Panel x="10" y="10" width="800" height="400" layout="absolute" horizontalAlign="right"
>
<mx:DataGrid x="20" y="20" id="dgPosts" width="400" verticalCenter="center" dataProvider="{feedRequest.lastResult.rss.xml}">
<mx:columns>
<mx:DataGridColumn headerText="标题" dataField="title" />
<mx:DataGridColumn headerText="时间" dataField="pubDate"/>

</mx:columns>
</mx:DataGrid>
<mx:TextArea x="20" y="175" width="400" height="100" htmlText="{dgPosts.selectedItem.description}"/>

</mx:Panel>


</mx:Application>
[/code]

好了!访问一下:http://localhost:8080/test/bin-debug/connect.html
截图如下:
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页