Flex和Jsp创建用户登入系统

 

在开始之前我们先来看下效果:【userName==password都是test】

这个例子中出了使用Flex与JSP验证用户登入之外呢,另外一个看点就是登入之后配合了states使用的resize效果来展示用户正确登入后的界面。

本文是根据Viper Creaitons中的Create a Login System with Flex and PHP 例子上把后台的php更改成JSP来实现的。【理由很简单,我不会PHP 】,后台数据的验证的方法是采用HttpService的,至于前台的参数传递使用的是<mx:Request>方式。网上找了下,还有一种方法是使用URLVariables对象。具体参考:http://blog.csdn.net/cjd007/archive/2007/05/25/1625823.aspx

先来看前台UserLogin.mxml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
  3.     <mx:states>  
  4.         <mx:State name="Logged In">  
  5.             <mx:SetProperty target="{panel1}" name="width" value="95%"/>  
  6.             <mx:SetProperty target="{panel1}" name="height" value="95%"/>  
  7.             <mx:RemoveChild target="{password}"/>  
  8.             <mx:RemoveChild target="{username}"/>  
  9.             <mx:RemoveChild target="{label1}"/>  
  10.             <mx:RemoveChild target="{Submit}"/>  
  11.             <mx:RemoveChild target="{label2}"/>  
  12.             <mx:SetProperty target="{panel1}" name="title" value="Today is Present"/>  
  13.             <mx:AddChild relativeTo="{panel1}" position="lastChild">  
  14.                 <mx:Label x="10" y="10" text="Today is a gift"/>  
  15.             </mx:AddChild>  
  16.             <mx:AddChild relativeTo="{panel1}" position="lastChild">  
  17.                 <mx:Label x="10" y="36" text="That's way we call it the present!"/>  
  18.             </mx:AddChild>  
  19.             <mx:AddChild relativeTo="{panel1}" position="lastChild">  
  20.                 <mx:Label x="10" y="62" text="Liceven"/>  
  21.             </mx:AddChild>  
  22.         </mx:State>  
  23.     </mx:states>  
  24.     <mx:Script>  
  25.         <![CDATA[  
  26.             import mx.rpc.events.ResultEvent;  
  27.               
  28.         ]]>  
  29.     </mx:Script>  
  30.     <mx:Script>  
  31.        
  32.   
  33. <![CDATA[  
  34. private function checkLogin(evt:ResultEvent):void  
  35. {  
  36.     if(evt.result.loginResult == "yes")  
  37.     {  
  38.     currentState = "Logged In";  
  39.     }  
  40.     if(evt.result.loginResult == "no")  
  41.     {  
  42.           
  43.         mx.controls.Alert.show('Invalid username/password');  
  44.     }          
  45. }  
  46. ]]>  
  47.   
  48. </mx:Script>  
  49.     <mx:HTTPService id="login_user" result="checkLogin(event)" showBusyCursor="true" method="POST" url="http://localhost:8080/UserLogin/loginCheck.jsp" useProxy="false">  
  50.         <mx:request xmlns="">  
  51.             <username>  
  52.                 {username.text}   
  53.             </username>  
  54.             <password>  
  55.                 {password.text}   
  56.             </password>  
  57.         </mx:request>  
  58.     </mx:HTTPService>  
  59.        
  60.     <mx:Panel resizeEffect="Resize" width="250" height="200" layout="absolute" title="Login System" horizontalCenter="0" verticalCenter="-2" id="panel1">  
  61.         <mx:Label x="10" y="10" text="Username:" id="label1"/>  
  62.         <mx:TextInput x="10" y="36" id="username"/>  
  63.         <mx:Label x="10" y="66" text="Password:" id="label2"/>  
  64.         <mx:TextInput x="10" y="92" id="password" displayAsPassword="true"/>  
  65.         <mx:Button x="10" y="122" label="Submit" id="Submit" click="login_user.send();"/>  
  66.     </mx:Panel>  
  67.        
  68. </mx:Application>  

 下面是后台loginCheck.jsp

Html代码 复制代码
  1. <%   
  2.     response.setContentType("text/xml");   
  3.     out.println("<?xml version=/"1.0/" encoding=/"utf-8/"?>");   
  4.     String userName = request.getParameter("username");   
  5.     String password = request.getParameter("password");   
  6.   
  7.     //Here we do a simple checking to make sure userName equals to password   
  8.   
  9.     //and then outprints yes or not   
  10.   
  11.     String loginResult = "<loginResult>";   
  12.     if (userName.equals(password)) {   
  13.         loginResult += "yes";   
  14.     } else {   
  15.         loginResult += "no";   
  16.     }   
  17.     loginResult += "</loginResult>";   
  18.   
  19.     out.println(loginResult);   
  20. %>  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值