加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex4_HttpService组件

发布时间:2020-12-15 04:10:46 所属栏目:百科 来源:网络整理
导读:http://www.cnblogs.com/lovemoon714/archive/2012/05/25/2517684.html 1、在JavaWeb项目中新建Servlet(FlexLoginServelt) : public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { respons

http://www.cnblogs.com/lovemoon714/archive/2012/05/25/2517684.html



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、效果图:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读