Flex4_HttpService组件

1、在JavaWeb项目中新建Servlet(FlexLoginServelt) :

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

        response.setContentType("text/html");
        response.setContentType("text/xml;charset=utf-8");
        PrintWriter out = response.getWriter();
        //接收Flex端传来的参数
        String userName=request.getParameter("userName");
        String userPwd=request.getParameter("userPwd");
        String result="登录失败,用户名或密码错误!";
        if("admin".equals(userName) || "".equals(userPwd)){
            result="登录成功!";
        }
        //将登录信息返回给客户端
        out.println(result);
        out.flush();
        out.close();
    }

2、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/mx" minWidth="955" minHeight="600"
               initialize="init()">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            
            private function init():void{
                btn_login.addEventListener(MouseEvent.CLICK,login);
                
                //加载HTTPService的返回监听
                httpSer.addEventListener(ResultEvent.RESULT,httpSerResultHandler);
                httpSer.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>
        <s:HTTPService id="httpSer" url="http://localhost:8090/FlexHttpService/FlexLoginServelt" method="POST">
            <s:request>
                <!--需要发送到服务器的参数名,及值,接收参数名时必须一致-->
                <userName>{txt_userName.text}</userName>
                <userPwd>{txt_userPwd.text}</userPwd>
            </s:request>
        </s:HTTPService>
    </fx:Declarations>
    <s:Panel x="37" y="40" width="250" height="200">
        <s:Label x="37" y="28" text="用户名:"/>
        <s:Label x="37" y="61" text="密    码:"/>
        <s:TextInput id="txt_userName" x="82" y="24"/>
        <s:TextInput id="txt_userPwd" x="83" y="56" displayAsPassword="true"/>
        <s:Button x="83" y="115" label="登录" id="btn_login"/>
    </s:Panel>
</s:Application>

3、效果图:

转载于:https://www.cnblogs.com/lovemoon714/archive/2012/05/25/2517684.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值