flex与servlet交互

本文介绍了一个简单的示例,展示了如何使用Eclipse创建一个Servlet,并通过FlexBuilder中的Flex项目调用该Servlet来获取XML数据,然后在Flex应用中显示这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,我们在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
截图如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值