環境: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>
<
//調用失敗
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>

工程文件:
點我下載工程文件