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

Flex4_HttpService组件

你可能感兴趣的:(service)