環(huán)境:myeclipse8.5+flex4+blazeds
描述:flex4中httpservice與服務(wù)器端交互的值傳遞問(wèn)題
方式一:通過(guò)<s:request/>標(biāo)簽進(jìn)行交互,在該標(biāo)簽內(nèi)部以要傳遞的參數(shù)名作為該標(biāo)簽內(nèi)的子標(biāo)簽,值作為內(nèi)容進(jìn)行傳遞,服務(wù)端接受數(shù)據(jù)采用request.getParmeter("參數(shù)名")獲取數(shù)據(jù).
示例代碼:
flex中的代碼:
<!--定義HttpService發(fā)送請(qǐng)求-->
<s:HTTPService id="service"
url="http://localhost:8080/testhttpservice/testHttpServiceServlet"
useProxy="false"
fault="service_faultHandler(event)"
result="service_resultHandler(event)">
<!--第一種傳值方式-->
<s:request >
<!--參數(shù)名稱作標(biāo)簽,中間填充參數(shù)值-->
<username>{txtusername.text}</username>
<password>{txtpassword.text}</password>
</s:request>
</s:HTTPService>
后臺(tái)接受參數(shù)的代碼:
//獲取flex傳遞的參數(shù) 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");
方式二:第二種傳值方式通過(guò)send()方法傳值send方法中傳遞參數(shù) ,服務(wù)端接受數(shù)據(jù)采用request.getParmeter("參數(shù)名")獲取數(shù)據(jù).
示例代碼:
//第二種傳值方式 通過(guò)send()方法傳值 send方法中傳遞參數(shù)
//定義一object對(duì)象
var val:Object=new Object();
//分別將文本框username,password的值傳遞到后臺(tái)
//object對(duì)象.參數(shù)名=值 傳值操作
val.username=txtusername.text;
val.password=txtpassword.text;
service.send(val);
貼出完整的代碼:
服務(wù)器端:
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交互參數(shù)傳遞探討<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");
//設(shè)置瀏覽器輸出字符編碼
response.setCharacterEncoding("utf-8");
PrintWriter writer=response.getWriter();
//獲取flex傳遞的參數(shù) 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");
//構(gòu)建一個(gè)list存放一些數(shù)據(jù)用來(lái)模擬用戶是否存在這一功能
List<String> list=new ArrayList<String>();
list.add("張三");
list.add("李四");
list.add("王五");
list.add("曹操");
list.add("孫權(quán)");
list.add("劉備");
//檢驗(yàn)用戶
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>
<
//調(diào)用失敗
protected function service_faultHandler(event:FaultEvent):void
{
Alert.show("失敗了:"+event.message,"提示");
}

//調(diào)用成功
protected function service_resultHandler(event:ResultEvent):void
{
Alert.show("成功了:"+event.result as String,"提示");
}
//調(diào)用
protected function button1_clickHandler(event:MouseEvent):void
{
//第一種傳值方式
//service.send();
//第二種傳值方式 通過(guò)send()方法傳值 send方法中傳遞參數(shù)
//定義一object對(duì)象
var val:Object=new Object();
//分別將文本框username,password的值傳遞到后臺(tái)
//object對(duì)象.參數(shù)名=值 傳值操作
val.username=txtusername.text;
val.password=txtpassword.text;
service.send(val);
}

]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對(duì)象)放在此處 -->
<!--定義HttpService發(fā)送請(qǐng)求-->
<s:HTTPService id="service"
url="http://localhost:8080/testhttpservice/testHttpServiceServlet"
useProxy="false"
fault="service_faultHandler(event)"
result="service_resultHandler(event)">
<!--第一種傳值方式-->
<s:request >
<!--參數(shù)名稱作標(biāo)簽,中間填充參數(shù)值-->
<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="發(fā)送" click="button1_clickHandler(event)"/>
<s:Label x="290" y="206" text="用戶名:"/>
<s:Label x="297" y="266" text="密碼:"/>
</s:Application>
工程文件:
點(diǎn)我下載工程文件
描述:flex4中httpservice與服務(wù)器端交互的值傳遞問(wèn)題
方式一:通過(guò)<s:request/>標(biāo)簽進(jìn)行交互,在該標(biāo)簽內(nèi)部以要傳遞的參數(shù)名作為該標(biāo)簽內(nèi)的子標(biāo)簽,值作為內(nèi)容進(jìn)行傳遞,服務(wù)端接受數(shù)據(jù)采用request.getParmeter("參數(shù)名")獲取數(shù)據(jù).
示例代碼:
flex中的代碼:





















示例代碼:








服務(wù)器端:


























































































































點(diǎn)我下載工程文件