1. Flex通过HttpService与服务端Servlet通信
flex代码:
<?xml version=”1.0″ encoding=”utf-8″?>
<s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009″
xmlns:s=”library://ns.adobe.com/flex/spark”
xmlns:mx=”library://ns.adobe.com/flex/halo”
minWidth=”1024″ minHeight=”768″>
<fx:Script>
<!–[CDATA[
import mx.controls.*;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.mxml.HTTPService;
protected function button1_clickHandler(event:MouseEvent):void
{
var addNew:HTTPService = new HTTPService();
addNew.resultFormat = "text" ;
addNew.method = "POST";
addNew.url = "http://localhost:8084/Flex_web/flex";
addNew.addEventListener(ResultEvent.RESULT,result_Handler);
var param:Object = new Object();
param.user = user.text;
param.pwd = pwd.text;
addNew.send(param);
}
public function result_Handler(event:ResultEvent):void
{
var mes:String = event.message.body.toString();
Alert.show("成功!" + mes,"信息");
}
]]–>
</fx:Script>
<fx:Declarations>
<!– Place non-visual elements (e.g., services, value objects) here –>
</fx:Declarations>
<s:Panel x=”200″ y=”110″ width=”224″ height=”157″ title=”登录系统”>
<s:Button x=”24″ y=”92″ label=”登录” click=”button1_clickHandler(event)” width=”171″ focusEnabled=”true”/>
<s:TextInput x=”67″ y=”10″ id=”user”/>
<s:TextInput x=”67″ y=”49″ id=”pwd”/>
<s:Label x=”20″ y=”15″ text=”用户名:”/>
<s:Label x=”20″ y=”55″ text=”密码:”/>
</s:Panel>
</s:Application>
servlet代码:
package advang;
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 flexServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8″);
String user = request.getParameter(“user”);
String pwd = request.getParameter(“pwd”);
user = new String(user.getBytes(“ISO8859-1″),”UTF-8″);
pwd = new String(pwd.getBytes(“ISO8859-1″),”UTF-8″);
PrintWriter out = response.getWriter();
try {
out.print(“从服务器返回的数据[" + user + "][" + pwd + "]!”);
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return ”Short description”;
}
}
2. Flex通过HttpService与服务端的XML通信
XML代码
<?xml version=”1.0″ encoding=”UTF-8″?>
<allUnits>
<unit>
<unitName>Red</unitName>
<unitId>1</unitId>
</unit>
<unit>
<unitName>Yellow</unitName>
<unitId>2</unitId>
</unit>
<unit>
<unitName>Green</unitName>
<unitId>3</unitId>
</unit>
<unit>
<unitName>Blue</unitName>
<unitId>4</unitId>
</unit>
<unit>
<unitName>Orange</unitName>
<unitId>5</unitId>
</unit>
</allUnits>
MXML代码:
<?xml version=”1.0″ encoding=”utf-8″?>
<!–初始化是调用inits()函数读取服务端的xml文件–>
<s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009″
xmlns:s=”library://ns.adobe.com/flex/spark”
xmlns:mx=”library://ns.adobe.com/flex/mx”
minWidth=”955″ minHeight=”600″
creationComplete=”inits()” >
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
//初始化函数 读取服务端的XML文件
protected function inits():void
{
UnitRPC.send();
}
//对返回的结果进行处理
protected function UnitRPC_resultHandler(event:ResultEvent):void
{
show.dataProvider=event.result.allUnits.unit;
}
]]>
</fx:Script>
<fx:Declarations>
<!–构建HttpService对象 指明请求地址和返回结果处理函数–>
<s:HTTPService id=”UnitRPC” url=”com/iman/demo/units.xml”
result=”UnitRPC_resultHandler(event)” />
</fx:Declarations>
<s:layout>
<s:VerticalLayout paddingTop=”20″ horizontalAlign=”center” verticalAlign=”top”/>
</s:layout>
<s:Panel title=”HttpService和XML的通信”>
<s:layout>
<s:VerticalLayout paddingTop=”20″ horizontalAlign=”center” verticalAlign=”top”/>
</s:layout>
<mx:DataGrid id=”show” >
<mx:columns>
<mx:DataGridColumn headerText=”名称” dataField=”unitName” />
<mx:DataGridColumn headerText=”ID” dataField=”unitId”/>
</mx:columns>
</mx:DataGrid>
</s:Panel>
</s:Application>