flex中httpservice与java后台交互的两种传值方式

环境:myeclipse8.5+flex4+blazeds
            描述:flex4中httpservice与服务器端交互的值传递问题
            方式一:通过<s:request/>标签进行交互,在该标签内部以要传递的参数名作为该标签内的子标签,值作为内容进行传递,服务端接受数据采用request.getParmeter("参数名")获取数据.
            示例代码:
            flex中的代码:
<!-- 定义HttpService发送请求 -->
        
< s:HTTPService  id ="service"  
                       url
="http://localhost:8080/testhttpservice/testHttpServiceServlet"  
                       useProxy
="false"  
                       fault
="service_faultHandler(event)"  
                       result
="service_resultHandler(event)" >
            
<!-- 第一种传值方式 -->
            
            
< s:request  >
                
<!-- 参数名称作标签,中间填充参数值 -->
                
< username > {txtusername.text} </ username >
                
< password > {txtpassword.text} </ password >
            
</ s:request >
            
        
</ s:HTTPService >
        后台接受参数的代码:
         // 获取flex传递的参数 username password
        String username = request.getParameter( " username " );
        
// get方式处理乱码
        
// username=new String(username.getBytes("ISO-8859-1"),"utf-8");
        String password = request.getParameter( " password " );
        
// password=new String(password.getBytes("ISO-8859-1"),"utf-8");
        方式二:第二种传值方式通过send()方法传值send方法中传递参数 ,服务端接受数据采用request.getParmeter("参数名")获取数据.
        示例代码:
                 //第二种传值方式 通过send()方法传值 send方法中传递参数 
                //定义一object对象
                var val:Object=new Object();
                //分别将文本框username,password的值传递到后台
                //object对象.参数名=值   传值操作
                val.username=txtusername.text;
                val.password=txtpassword.text;
                service.send(val);
        贴出完整的代码:
        服务器端:
package  com.servlet;

import  java.io.IOException;
import  java.io.PrintWriter;
import  java.util.ArrayList;
import  java.util.List;

import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

/**
 * 功能描述:flex httpservice与java交互参数传递探讨<br>
 * 
@author sxyx2008<br>
 * @date 2010-07-19
 *
 
*/

@SuppressWarnings(
" serial " )
public   class  TestHttpServiceServlet  extends  HttpServlet  {
    
    @Override
    
protected void service(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        
//处理post方式乱码
        request.setCharacterEncoding("utf-8");
        
//设置浏览器输出字符编码
        response.setCharacterEncoding("utf-8");
        PrintWriter writer
=response.getWriter();
        
//获取flex传递的参数 username password
        String username=request.getParameter("username");
        
//get方式处理乱码
        
        
//username=new String(username.getBytes("ISO-8859-1"),"utf-8");
        String password=request.getParameter("password");
        
//password=new String(password.getBytes("ISO-8859-1"),"utf-8");
        
        
//构建一个list存放一些数据用来模拟用户是否存在这一功能
        List<String> list=new ArrayList<String>();
        list.add(
"张三");
        list.add(
"李四");
        list.add(
"王五");
        list.add(
"曹操");
        list.add(
"孙权");
        list.add(
"刘备");
        
//检验用户
        if(list.contains(username)){
            writer.print(
"存在:"+username+"客户端传递的密码是:"+password);
        }
else{
            writer.print(
"找不到:"+username+"客户端传递的密码是:"+password);
        }

        
    }

}

        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" >
    
< fx:Script >
        
<![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;

            //调用失败
            protected function service_faultHandler(event:FaultEvent):void
            {
                Alert.show("失败了:"+event.message,"提示");
            }

            //调用成功
            protected function service_resultHandler(event:ResultEvent):void
            {
                Alert.show("成功了:"+event.result as String,"提示");
            }
    
            //调用
            protected function button1_clickHandler(event:MouseEvent):void
            {
                //第一种传值方式                
                //service.send();
                
                //第二种传值方式 通过send()方法传值 send方法中传递参数 
                //定义一object对象
                var val:Object=new Object();
                //分别将文本框username,password的值传递到后台
                //object对象.参数名=值   传值操作
                val.username=txtusername.text;
                val.password=txtpassword.text;
                service.send(val);
            }

        
]]>
    
</ fx:Script >
    
< fx:Declarations >
        
<!--  将非可视元素(例如服务、值对象)放在此处  -->
        
<!-- 定义HttpService发送请求 -->
        
< s:HTTPService  id ="service"  
                       url
="http://localhost:8080/testhttpservice/testHttpServiceServlet"  
                       useProxy
="false"  
                       fault
="service_faultHandler(event)"  
                       result
="service_resultHandler(event)" >
            
<!-- 第一种传值方式 -->
            
            
< s:request  >
                
<!-- 参数名称作标签,中间填充参数值 -->
                
< username > {txtusername.text} </ username >
                
< password > {txtpassword.text} </ password >
            
</ s:request >
            
        
</ s:HTTPService >
        
        
    
</ fx:Declarations >
    
< s:TextInput  x ="332"  y ="196"  id ="txtusername" />
    
< s:TextInput  x ="332"  y ="256"  id ="txtpassword"  displayAsPassword ="true" />
    
< s:Button  x ="357"  y ="311"  label ="发送"  click ="button1_clickHandler(event)" />
    
< s:Label  x ="290"  y ="206"  text ="用户名:" />
    
< s:Label  x ="297"  y ="266"  text ="密码:" />
</ s:Application >

你可能感兴趣的:(flex中httpservice与java后台交互的两种传值方式)