Flex vs. JSON and XML interoperability

Source: Internet
Author: User
Tags cdata command line curl expression json xml example client

Flex is to Java, like the beauty of the brain, or is there another argument? Who can tell me? All I know is that Flex and Java really work together to create incredibly rich Internet applications (RIA). What are you going to ask about flex? Flex is an open source framework that allows you to build flash applications through the Mxml language (and ActionScript 3).

See:Jack's speech on Flex and JSON and XML Interoperability (QuickTime format, 33MB).

Http://www.jackherrington.com/screencasts/FlexAndJSON.mov

You can download the Flex IDE from Adobe's site (Http://adobe.com/flex), the so-called Flex Builder, and start your development trip. Flex Builder is a commercial product, but it has a long, free trial phase that gives you enough time to figure out if it's worth the money. In this article, I'll show you how to work with Flex and Java together. Java will run on the server side, while flex runs on the client. The communication protocol between the two ends can be any agreement you want. But here, I'll use XML first, and then use JSON, because these two technologies are the most common in the Web 2.0 world.

Creating server Code

The XML example starts with the simple JSP file shown in Listing 1:

list 1. xml.jsp
<jsp:root xmlns:jsp= "Http://java.sun.com/JSP/Page" version= "1.2"
<jsp: Directive.page import= "java.text.*"/>
<jsp:directive.page import= "java.lang.*"/>
<jsp: Directive.page contenttype= "Text/xml"/>
<days><jsp:scriptlet>
<![ cdata[
Double compa = 1000.0
Double compb = 900.0;
for (int i = 0; i<=30; i++) {
Compa + = (math.rando M () * 100)-50;
COMPB + + (math.random () * 100)-50;
]]>
</jsp:scriptlet>
<day>
<num><jsp:expression>i</jsp:expression ></num>
<compa><jsp:expression>compa</jsp:expression></compa>
<COMPB ><jsp:expression>compb</jsp:expression></compb>
</day>
<jsp:scriptlet>
<! [cdata[}
]]>
</jsp:scriptlet>
</days>
</jsp:root>

This service will export some random stock data to two companies (Compa and COMPB) every 30 days. The first company started with a value of 1000 dollars, and the second started with 900 dollars, and the JSP code added a random number to the two values each day.

When I use the Curl client to access this service from the command line, I get the following result:

% curl "http://localhost:8080/jsp-examples/flexds/xml.jsp"
<days><day><num>0</num><compa>966.429108587301</compa>
<compb>920.7133933216961</compb>
</day>...</days>

The root tag is a <days> tag that contains a collection of <day> tags. Each <day> label has a <num> tag that represents the number of days, a <compa> value representing the stock price of company A, and the <compb> value to represent the stock price of Company B. The value of two shares varies with each request because they are randomly generated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.