Flex与Java低数据量通信息一:HTTPService

学习了一下Flex,想总结一下Flex与Java通信的例子。其中Flex与Java高数据量的通信用的是Blazeds,大多的例子是Blazeds自带的实例整理的。

首先看看使用Flex中的HTTPService如何与Java的Servlet通信。

Java端如下:

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 7780152800409281311L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("userpassword");
		String message = null;
		if (null == username || null == password || "".equals(username)
				|| "".equals(password)) {
			message = "failure";
		} else if ("mengya".equals(username) && "mengya".equals(password)) {
			message = "succeed";
		} else {
			message = "failure";
		}
		//返回客户端的是一个xml
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		out.print("<result>");
		out.print("<message>");
		out.print(message);
		out.print("</message>");
		out.print("</result>");
	}
}

与AJAX有点类似。

Flex端如下(主具的设计可以下载下面的源码):

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
				backgroundGradientAlphas="[1.0, 1.0]" 
				backgroundGradientColors="[#FD1D06, #EAF807]" height="328" width="428">
					
  	<mx:HTTPService 
  		showBusyCursor="true"  
  		id="loginSrv" 
  		result="doResult();"  
  	    method="GET"
  		url="http://localhost:8080/FlexStudy/LoginServlet"> 
 		<mx:request> 
 			<username>  
 				{txtname.text} 
 			</username> 
 			<userpassword> 
				{txtpwd.text}
 			</userpassword> 
 		</mx:request> 
	</mx:HTTPService>

	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private function doResult():void
			{
				var returnValue:String=loginSrv.lastResult.result.message; 
       			if(returnValue=="succeed") {  
 					this.currentState="login";
        		}  
 				else {  
					Alert.show("您的登录失败了","提示信息",Alert.OK,this,null,null,Alert.YES); 
              	}
			}
		]]>
	</mx:Script>

	<mx:Panel id="loginPanel" x="69.5" y="52" width="289" height="200" layout="absolute" title="登陆" fontSize="12">
		<mx:Button x="110" y="108" label="提交" id="btnSubmit" click="loginSrv.send();"/>
		<mx:TextInput x="79" y="30" fontSize="12" id="txtname"/>
		<mx:TextInput x="79" y="62" id="txtpwd" displayAsPassword="true"/>
		<mx:Label x="21" y="32" text="姓名:" id="lbname" fontSize="12" fontWeight="bold"/>
		<mx:Label x="21" y="64" text="密码:" id="lbpwd" fontSize="12" fontWeight="bold"/>
	</mx:Panel>
	
</mx:Application>

 

 

 

 

你可能感兴趣的:(java,Ajax,xml,Flex,Adobe)