Flex中HTTPService与java后台通信

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

方式一,前端mxml文件

<fx:Declarations>
		<!--url代表请求路径,resultFormat代表返回格式,result是声明回调函数,其中
		myFault(event)的event是Flex自动给我们填充进去的-->
		<s:HTTPService id="myHttpService" 
						method="GET"
						resultFormat="text"
						useProxy="false"
						fault="myFault(event)" 
						result="myResult(event)"
						url="http://127.0.0.1:8080/mytest/HttpService"> 
			<!--userName代表参数名称,{this.username.text}代表id为username的
			文本框中的值,括号{}代表的意思是绑定-->
			<!--<s:request xmlns=""> 
				<userName>张三</userName> 
				<userPwd>zs</userPwd> 
			</s:request>--> 
		</s:HTTPService> 
	</fx:Declarations>
	<fx:Script>
   
		<![CDATA[ 
			import mx.rpc.events.FaultEvent; 
			import mx.controls.Alert; 
			import mx.rpc.events.ResultEvent; 
			private function myFunction():void{ 
				//myHttpService.send(); 
				/*多行注释*/
				var val:Object = new Object();
				val.userName = "张三";
				val.userPwd = "zs";
				myHttpService.send(val);
			} 
			private function myResult(event:ResultEvent){ 
				var msg = event.result.toString(); 
				Alert.show(msg); 
			} 
			private function myFault(event:FaultEvent){ 
				var msg = event.fault.toString(); 
				Alert.show("错误:"+msg); 
			} 
		]]> 
	</fx:Script>
	<mx:Button id="myButton" x="160" y="136" width="100" height="40" label="点击" click="myFunction()"/>

后端servlet

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8;");
		PrintWriter out = response.getWriter();
		//接收请求的userName
		String userName = request.getParameter("userName");
		//get方式处理乱码
		userName = new String(userName.getBytes("ISO-8859-1"),"UTF-8");
		//接收请求的password
		String userPwd = request.getParameter("userPwd");
		userPwd = new String(userPwd.getBytes("ISO-8859-1"),"UTF-8");
		out.print("你输入的姓名为:" + userName + "  密码为:" + userPwd);
	}

方式二,前端mxml文件

	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.http.HTTPService;
			
			[Bindable]
			private var selectResponse:String = null;
			//[Bindable]类似于注解,作用是当arrayList的值一旦发生变化,表单DropDownList
			//的值也会发生变化
			[Bindable]
			private var arrayList:ArrayCollection = new ArrayCollection([
				{name:"CN",data:"中文"},{name:"US",data:"英文"}
			]);
			
			private function dropdownListChangeHandler(event:Event = null):void{
				var httpService:HTTPService = new HTTPService();
				httpService.resultFormat = "text";
				httpService.url = "http://localhost:8080/mytest/HttpService02";
				httpService.method = "POST";
				
				var param:Object = new Object;
				//object对象.参数名=值   传值操作
				param.selects = select.selectedItem.data;
				
				httpService.send(param);
				
				httpService.addEventListener(ResultEvent.RESULT,successHandler);
				httpService.addEventListener(FaultEvent.FAULT,faultHandler);
				
			}
			private function successHandler(event:ResultEvent):void{
				selectResponse = event.message.body.toString();
				languages.text = selectResponse;
				//Alert.show(selectResponse);
			}
			private function faultHandler(event:FaultEvent):void{
				selectResponse = event.message.body.toString();
				languages.text = selectResponse;
				//Alert.show(selectResponse);
			}
		]]>
	</fx:Script>
	<s:DropDownList id="select" change="dropdownListChangeHandler(event)"
					dataProvider="{this.arrayList}" labelField="name" selectedIndex="0"/>
	<mx:Text id="languages" text=""/>

后端servlet
<span style="white-space:pre">	</span>public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8;");
		PrintWriter out = response.getWriter();
		
		String selects = request.getParameter("selects");
		out.print("选择:" + selects);
	}


web.xml

<span style="white-space:pre">	</span><servlet>
		<servlet-name>HttpService</servlet-name>
		<servlet-class>com.lxz.HttpService</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HttpService</servlet-name>
		<url-pattern>/HttpService</url-pattern>
	</servlet-mapping>
	<servlet>
		<servlet-name>HttpService02</servlet-name>
		<servlet-class>com.lxz.HttpService02</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HttpService02</servlet-name>
		<url-pattern>/HttpService02</url-pattern>
	</servlet-mapping>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值