Flex JSP with HttpService

1. Create Flex simple application with Flex Builder 3

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  backgroundColor="#FFFFFF">
 <mx:Script>
  <![CDATA[
   import mx.collections.ArrayCollection;
   import mx.rpc.events.ResultEvent;
   
   [Bindable]
   private var employeeData:ArrayCollection;
   private function resultHandler(event:ResultEvent):void{
    employeeData = event.result.employees.employee;
   }
  ]]>
 </mx:Script>
 
 <mx:HTTPService id="employeeService" url="http://localhost:8080/FlexServer/employees.jsp"
   result="resultHandler(event)"/>
  
 <mx:DataGrid dataProvider="{employeeData}" width="100%" height="264" y="36"/>
 <mx:Button click="employeeService.send()" y="308" label="Refresh" horizontalCenter="15"/>
 <mx:Label y="10" text="用户列表" horizontalCenter="-14" fontWeight="bold" color="#0C0B3C" fontSize="18" fontFamily="Arial"/>
</mx:Application>

 

2. Create Static web project FlexServer and one JSP page named employees.jsp with Eclipse J2ee ganymede

<?xml version="1.0" encoding="utf-8"?>
<%@ page contentType="text/xml; charset=gb2312"%>
<%
 response.setHeader("Pragma", "No-cache");
 response.setHeader("Cache-Control", "no-cache");
 response.setDateHeader("Expires", 0);
%>
<%@ page language="java"%>
<%@ page import="com.mysql.jdbc.Driver"%>
<%@ page import="java.sql.*"%>

<%
 String driverName = "com.mysql.jdbc.Driver";
 String userName = "root";
 String userPasswd = "";
 String dbName = "test";
 String tableName = "employee";

 String url = "jdbc:mysql://localhost/" + dbName + "?user="
   + userName + "&password=" + userPasswd;
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 Connection connection = DriverManager.getConnection(url);
 Statement statement = connection.createStatement();
 String sql = "SELECT * FROM " + tableName;
 ResultSet rs = statement.executeQuery(sql);
 ResultSetMetaData rmeta = rs.getMetaData();
 int numColumns = rmeta.getColumnCount();
 out.write("<employees>");
 while (rs.next()) {

  out.write("<employee>");
  out.write("<name>");
  out.write(rs.getString("name"));
  out.write("</name>");
  out.write("<street>");
  out.write(rs.getString("street"));
  out.write("</street>");
  out.write("<city>");
  out.write(rs.getString("city"));
  out.write("</city>");
  out.write("<state>");
  out.write(rs.getString("state"));
  out.write("</state>");
  out.write("<zip>");
  out.write(rs.getString("zip"));
  out.write("</zip>");
  out.write("</employee>");

 }
 out.write("</employees>");
 rs.close();

 statement.close();
 connection.close();
%>

 

3. Mysql DB script

--
-- 数据库: `test`
--

-- --------------------------------------------------------

--
-- 表的结构 `employee`
--

CREATE TABLE IF NOT EXISTS `employee` (
`name` varchar(20) NOT NULL,
`street` varchar(100) NOT NULL,
`city` varchar(20) NOT NULL,
`state` varchar(20) NOT NULL,
`zip` varchar(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- 导出表中的数据 `employee`
--

INSERT INTO `employee` (`name`, `street`, `city`, `state`, `zip`) VALUES
('tom', 'Tianshan', 'Shanghai', 'China', '200000'),
('peter', '
Tianshan', 'Shanghai', 'China', '200000'),
('Mad Li', '
Tianshan', 'Shanghai', 'China sh', '200000'),
('test', 'MM', 'TT', 'kk', 'test');

 

4. mysql jdbc driver

mysql-connector-java-5.0.8-bin.jar

 

5. mysql

embedded in Xampp1.7

 

Appendix: about remoteObject usage with BladeDS

http://bbs.actionscript3.cn/thread-9982-1-1.html

你可能感兴趣的:(Flex JSP with HttpService)