1、采用<s:HTTPService>标签来实现:
<?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/mx" minWidth="955" minHeight="600" >
<s:Label x="61" y="138" text="HttpService:"/>
<s:TextInput x="152" y="128" > </s:HTTPService>
</fx:Declarations>
</s:Application>
2、采用创建HTTPService对象:
<?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/mx" minWidth="955" minHeight="600" >
<s:Label x="61" y="138" text="HttpService:"/>
<s:TextInput x="152" y="128" />
<fx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.http.HTTPService;
protected function button2_clickHandler(event:MouseEvent):void
{
var http:HTTPService = new HTTPService();
http.url = "http://localhost:8080/html/FlexServlet0?name="+http_txt.text;
http.send();
http.addEventListener(ResultEvent.RESULT,httpSerResultHandler);
http.addEventListener(FaultEvent.FAULT,httpSerFaultHandler);
}
private function login(event:MouseEvent):void{
httpSer.send();//发送请求
}
//返回成功事件
private function httpSerResultHandler(event:ResultEvent):void{
Alert.show(event.result.toString(),"登录提示");
}
//返回失败事件
private function httpSerFaultHandler(event:FaultEvent):void{
Alert.show(event.fault.message as String,"登录提示");
}
]]>
</fx:Script>
<fx:Declarations>
</fx:Declarations>
</s:Application>
3、在JavaWeb项目中新建Servlet(FlexServlet) :
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.write("你好["+name+"]这是来自Httpservice的消息...当前Session是:"+request.getSession());
pw.close();
}