In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you the example analysis of the interaction between Flex and Java Servlet. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
In order to find out how Flex communicates with the background, I can be said to have done nothing in the first five days of learning Flex. The purpose of this article is to make beginners take fewer detours.
Principle of interaction: three letters-XML, the client accepts XML data sent from the server.
Workflow: the client is simply a DataGrid component, which is used to display the data transmitted from the server. The corresponding mxml file is as follows:
XML code
xml version= "1.0" encoding= "UTF-8"? > < mx:Application xmlns:mx= "http://www.adobe.com/2006/mxml" layout=" absolute "> < mx:Model source=" http://localhost:8080/flex/first" id= "model" > < / mx:Model > < mx:Panel title= "user Information" width= "776" height= " < fontSize= "18" > < mx:DataGrid dataProvider= "{model.user}" width= "748" height= "231" > < mx:columns > < mx:DataGridColumn dataField= "name" headerText= "user" > < / mx:DataGridColumn > < mx:DataGridColumn dataField= "pwd" headerText= "password" > < / mx:DataGridColumn > < mx:DataGridColumn dataField= "school" headerText= "School now" < / mx:DataGridColumn > < / mx:columns > < / mx:DataGrid > < / mx:Panel > < / mx:Application >
Note the < mx:Model > tag here. The source attribute specifies a servlet mapping, and the purpose of this servlet is to write XML to the client using response.
Read database data, generate XML file consists of two classes, one is FirstServlet.java, the other is XML.java, where the former is a common servlet, used to write XML files, the latter is specially used to generate XML files, completed by Java XML API operations. Cut the crap and look at the code first.
FirstServlet Code Summary:
Java code
Try {xml.init (); Class.forName ("com.microsoft.jdbc.sqlserver.SQLServerDriver"); / / * create a database named flex*// String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=flex" / / * replace your own username and password information * / / con = DriverManager.getConnection (url, "sa", "135780"); stmt = con.createStatement () / / * for more information on the SQL statement named USERS, please see attachment download * / / result = stmt.executeQuery ("select * from USERS");} catch (Exception e) {e.printStackTrace () } / / important: set the response format to XML format response.setContentType ("text/xml"); response.setCharacterEncoding ("UTF-8"); PrintWriter out = response.getWriter ()
The above is the code for servlet to connect to the database and format the response. Here is the key code for writing the XML file:
Java code
While (result.next ()) {String [] strs = new String [3]; strs [0] = result.getString ("name"); strs [1] = result.getString ("pwd"); strs [2] = result.getString ("school") / / * create XMLdocument*// xml.create (strs);} result.close (); stmt.close (); con.close () / / * write XML file to client * / / xml.output (out)
The xml in the last line is an instance of XML.java. Some important methods of XML are as follows:
Java code
/ * write the XML document to the output stream * * @ param out *-specified output stream * @ throws Exception * * author: Wang Jinghui Hunan Agricultural University & Hunan iRuijie Investment Management Company * / public void output (Writer writer) throws Exception {Transformer trans = TransformerFactory.newInstance () .newTransformer () Trans.setOutputProperty (OutputKeys.ENCODING, "UTF-8"); Source source = new DOMSource (document); Result result = new StreamResult (writer); trans.transform (source, result); writer.flush (); writer.close ();}
Java code
/ * create XML document * * @ param strs *-name and password and school parameters sent * * author: Wang Jinghui Hunan Agricultural University & Hunan iRuijie Investment Management Company * / public void create (String [] strs) {/ / * level child node * / / Element first = document.createElement ("user") Root.appendChild (first); for (int iTuno; I < strs.length; iTunes +) {if (iTune0) {/ / * second-level child node * / / Element name = document.createElement ("name") Name.appendChild (document.createTextNode (strs [I])); first.appendChild (name);} if (iTunes 1) {Element pwd = document.createElement ("pwd"); pwd.appendChild (document.createTextNode (strs [I])) First.appendChild (pwd);} if (iTunes 2) {Element school = document.createElement ("school"); school.appendChild (document.createTextNode (strs [I])); first.appendChild (school) }
In fact, in the final analysis, there is nothing mysterious about this example, as long as remember that XML is the medium of data exchange between Flex and the background, no matter how ever-changing the background data, there is this one. If you experiment, you can start with simplicity, do not involve querying the database, and write XML directly in servlet.
If you have any questions, please feel free to leave a message. I am also a beginner. Let's make progress together.
The method of running the program:
STEP1: create a database flex in SQLServer2000, and then import the attached SQL statement .sql in query Analyzer to generate the required tables.
STEP2: extract the flex.war under the FlexDataService installation directory (for example, D:\ a), then copy the entire flex directory under D:\ a\ web-inf to the WEB-INF of the WEB program, and copy the jar package under the D:\ a\ web-inf\ lib directory to the lib directory under the corresponding WEB program.
STEP4: copy project files to the MyEclipse project you built
Because there are a lot of files involved in Flex's communication with Java, it would be nice if you could deploy it before. In general, the deployment can be successful according to the above steps.
The above is an example analysis of the interaction between Flex and Java Servlet. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.