flex之HTTPService 对象

阅读更多
注:原创作品,分享为交流学习,转载请注明出处。

     HTTPService对象的使用 不用任何的额外配置,直接访问j2ee的Servlet类。从servlet返回来的数据类型有如下几种:
    object、array、xml、flashvars、text和e4x,默认的设置为object。

下面介绍六种返回类型的实例
第一种:返回类型是e4x(实例是简单计算器)  
   ECMAScript for XML(E4X)是一扩展了ECMAScript(包含ActionScript、DMDScript、JavaScript、JScript等)的编程语言,它增加对 XML 的内在支持。它的目标是在访问XML文档时,提供一种更直观、语法更简洁的的 DOM 接口。它也是处理XML时的一种新的方式。在 E4X 版本之前,通常总是在 Objcet(对象)层次上访问XML,而 E4X 则是把 XML 当做原始数据(primitive)来处理(比如字符、数字和布尔值)。这也意味着更快的访问速度、更好的支持、和使 XML 可以成为程序的一个组块(数据结构)。
e4x实例:
var sales = 
     
     
     
   ;
 
 alert( sales.item.(@type == "carrot").@quantity );
 alert( sales.@vendor );
 for each( var price in sales..@price ) {
   alert( price );
 }


flex页面:
  
  
	
	
	  
		  
		  
	  
	  
		  
	  
	  
		  
			  
				    
				  
				  
				  
				  
			  
			  
				  
				  
			  
		  
	  
  


服务端Servlet:
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FlexCalculate extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println("number1:" + request.getParameter("number1"));
		System.out.println("number2:" + request.getParameter("number2"));
		System.out.println("opertion:" + request.getParameter("opertion"));
		int result = 0;
		if (request.getParameter("opertion").equals("+"))
		{
			result = Integer.valueOf(request.getParameter("number1"))
					+ Integer.valueOf(request.getParameter("number2"));
		} else if (request.getParameter("opertion").equals("-"))
		{
			result = Integer.valueOf(request.getParameter("number1"))
					- Integer.valueOf(request.getParameter("number2"));
		} else if (request.getParameter("opertion").equals("*"))
		{
			result = Integer.valueOf(request.getParameter("number1"))
					* Integer.valueOf(request.getParameter("number2"));
		} else
		{
			result = Integer.valueOf(request.getParameter("number1"))
					/ Integer.valueOf(request.getParameter("number2"));
		}
		System.out.println("result1:" + result);

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=utf-8");
		String para = request.getParameter("username");
		PrintWriter out = response.getWriter();
		out.println("");
		out.println("" + result + "");
		out.println("");
		out.flush();
		out.close();
	}

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



第二种:返回类型是list
flex端:

	  
		  
		  
	  

	this.sevlet.send(param);//  

	private function resultHandler(event:ResultEvent):void{
				trace(event.result);
//				result.text = event.result.calculateResult  
				this.submit.enabled=true;  
	}  


java服务端:
package controller;
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;
public class StringListServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		List resultList = new ArrayList();
		resultList.add("chenchaoyang");
		resultList.add("supan");
		resultList.add("pangzi");
		resultList.add("27");
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println(resultList);
		out.flush();
		out.close();
	}

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


你可能感兴趣的:(flex)