The earth has been moved to a vibrating state. Use the Httpservice method in flex to interact with Java:
First, write the server:
1. Building a Web project in MyEclipse
2. Write a servlet to print XML
3. Do not overwrite remoting-config.xml files when using Httpservice to interact with Java
4.web.xml file to configure the access address of the servlet (generally not configured, when a servlet is built MyEclipse will automatically configure the Web.xml file, do not have to change, but it should be noted that When you import blazeds development file, he will change Web.xml file, at this time you must be web.xml file change, otherwise flex in access to the server when you can not ask your servlet.
5. When your server-side servlet is written, then build the Flex project and configure the Flex server's access address in the Flex project's property entries based on your previously built servlet project.
6. Write Flex mxml files.
Second simple case:
1.servlet Source Code
Copy Code code as follows:
Package Com.wle.flex.HttpService.java File Source code
Package Com.wle.flex;
Import java.io.IOException;
Import Java.io.Writer;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class HttpService01 extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/xml;charset=utf-8");
Writer out = Response.getwriter ();
Out.write ("<?xml version= ' 1.0 ' encoding= ' utf-8 '?> ')";
Out.write ("<item>");
Out.write ("<student name= ' Lin ' age= ' class= ')" Water park Liangshan Yi class '/> ');
Out.write ("<student name= ' Likui ' age= ' class= ')" Water park Liangshan second class '/> ');
Out.write ("<student name= ' Sansang ' age= ' class= ')" Water park Liangshan a class '/> ');
Out.write ("<student name= ' when moved ' age= ' class= '" Water park Liangshan second class '/> ');
Out.write ("<student name= ' Wu Song ' age= ') ' class= ' Water park Liangshan Yi class '/>");
Out.write ("<student name= ' 燕青 ' age= ' class= ')" Water park Liangshan second class '/> ');
Out.write ("</item>");
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
This.doget (request, response);
}
}
2.servlet corresponding Web.xml file
Copy Code code as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "3.0 "
xmlns=" Http://java.sun.com/xml/ns/javaee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
XSi : schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >
<display-name></display-name>
<servlet>
<description>this is the description of M Y-java component</description>
<display-name>this is the display name of my Java EE component</display-n Ame>
<servlet-name>HttpService01</servlet-name>
<servlet-class> Com.wle.flex.httpservice01</servlet-class>
</servlet>
<servlet-mapping>
<ser Vlet-name>httpservice01</servlet-name>
<url-pattern>/wl/HttpService01</url-pattern>
</servlet-mapping>
</web-app>
3. Import blazeds files in the Webroot directory
4. Publish the Servlet project
For example, the Servlet project is published to the D:\Tomcat 6.0\webapps\flex_service_httpservice file.
5. Build Flex Project
After you have built the Flex project, right-click the Select attribute on the project and set the Flex server's address in the Flex server option in the attribute
The configuration information is as follows:
File root directory: D:\Tomcat 6.0\webapps\flex_service_httpservice
Root URL (U): Http://localhost:8089/flex_service_HttpService
Context Root:/flex_service_httpservice
6. After you configure the server for the Flex project, start writing the flex source file as follows:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml"
layout= "Vertical"
Fontsize= "Viewsourceurl=" "srcview/index.html" xmlns:s= "Library://ns.adobe.com/flex/spark" >
<!--<mx:style source= "Dgtoexcel.css"/>-->
<mx:Script>
<! [cdata[
Import mx.collections.ArrayCollection;
Import Mx.controls.Alert;
Import mx.events.FlexEvent;
Import mx.events.ItemClickEvent;
Import mx.rpc.events.FaultEvent;
[bindable]
private Var myac:arraycollection;
Private Function Faulthandler (event:faultevent): void
{
Alert.show (event.fault.faultString, event.fault.message);
}
Function to filter out all items with gender
Private Function Malefilterfunc (item:object): Boolean {
return Item.gender = = 1;
}
function to apply the filter function the Icollectionview.
Private Function Filtermale (): void {
Myac.filterfunction = Malefilterfunc;
Refresh the collection view to apply the filter.
Myac.refresh ();
}
Function to filter out all items with gender
Private Function Femalefilterfunc (item:object): Boolean {
return Item.gender = = 0;
}
function to apply the filter function the Icollectionview.
Private Function Filterfemale (): void {
Myac.filterfunction = Femalefilterfunc;
Refresh the collection view to apply the filter.
Myac.refresh ();
}
Function to Reset the view to its original state.
Private Function Resetac (): void {
Myac.filterfunction = null;
Refresh the collection view.
Myac.refresh ();
}
Event handler function to display the selected button
Private Function Filterhandler (event:itemclickevent): void {
Switch (event.currentTarget.selectedValue) {
Case 1:
Filtermale ();
Break
Case 0:
Filterfemale ();
Break
Case 2:
Resetac ();
Break
Default
Break
}
}
protected function Bt1_clickhandler (): void
{
Myservice.send ();
}
]]>
</mx:Script>
<mx:httpservice id= "MyService"
Showbusycursor= "true"
Url= "HTTP://LOCALHOST:8089/FLEX_SERVICE_HTTPSERVICE/WL/HTTPSERVICE01"
result= "MYAC = event.result.item.student"
fault= "Faulthandler (event)"
Contenttype= "Application/xml"
/>
<mx:HBox>
<mx:radiobuttongroup id= "Gendertype" itemclick= "Filterhandler" (event); />
<mx:radiobutton groupname= "Gendertype" id= "Rbmale" value= "1" label= "male"/>
<mx:radiobutton groupname= "Gendertype" id= "Rbfemale" value= "0" label= "female"/>
<mx:radiobutton groupname= "Gendertype" id= "Rball" value= "2" label= "All"/>
</mx:HBox>
<mx:datagrid id= "MYDG" width= "100%" rowcount= "20"
Dataprovider= "{Myac}" >
<mx:columns>
<mx:datagridcolumn headertext= "name" datafield= "name"/>
<mx:datagridcolumn headertext= "Age" datafield= "ages"/>
<mx:datagridcolumn headertext= "Class" Datafield= "Class"/>
</mx:columns>
</mx:DataGrid>
<mx:HBox>
<s:button id= "BT1" label= "Submit" click= "Bt1_clickhandler ()"/>
</mx:HBox>
</mx:Application>
This enables the interaction of flex to Java EE