Here is an example to post data from flex to JSP using httpservice. We’ll put a simple form under the data grid above to enter the data about the new employee and Pressing the Add Employee button will submit the entered data to the JSP, which will attach them to existing employees and return back so the data grid can be repopulated to include the newly inserted employee.
Here is code for Employee entry and Data grid.
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” applicationComplete=”employees.send()”><mx:Script>
<![CDATA[
import mx.events.ValidationResultEvent;
import mx.controls.Alert;private function validateEmpName():void{
if (empNameValidator.validate().type == ValidationResultEvent.VALID)
{
submitForm();
}
else
{
Alert.show("Employee name can not be blank");
}
}
private function submitForm():void
{
employees.cancel();
employees.send(employeeModel);
}
private function onResult(event:Event):void
{
return;
}
]]>
</mx:Script>
<mx:Model id=”employeeModel”>
<root>
<empName>{empName.text}</empName>
<age>{age.text}</age>
<skills>{skills.text}</skills>
</root>
</mx:Model>
<mx:HTTPService id=”employees” useProxy=”false” method=”POST” url=”employee.jsp”/>
<mx:DataGrid dataProvider=”{employees.lastResult.people.person}” width=”50%”>
<mx:columns>
<mx:DataGridColumn dataField=”name” headerText=”Name” />
<mx:DataGridColumn dataField=”age” headerText=”Age”/>
<mx:DataGridColumn dataField=”skills” headerText=”Skills”/>
</mx:columns>
</mx:DataGrid>
<mx:StringValidator id=”empNameValidator” source=”{empName}” property=”text” triggerEvent=”"/>
<mx:Form width=”100%” height=”100%” x=”50″ y=”400″>
<mx:FormItem label=”Enter name:” required=”true”>
<mx:TextInput id=”empName” />
</mx:FormItem>
<mx:FormItem label=”Enter age:”>
<mx:TextInput id=”age” />
</mx:FormItem>
<mx:FormItem label=”Enter skills”>
<mx:TextInput id=”skills” />
</mx:FormItem>
<mx:Button label=”Add Employee” click=”validateEmpName()”/>
</mx:Form>
</mx:Application>
———————————————————————
here is code for employee.jsp
———————————————————————
<%
String employees=”<?xml version=\’1.0\’ encoding=\’UTF-8\’?><people><person><name>Alex</name><age>22</age><skills>java, HTML, SQL</skills></person><person><name>Brandon Smith</name><age>21</age><skills>PowerScript, JavaScript, ActionScript</skills></person><person><name>Jeremy Plant</name><age>20</age><skills>SQL, C++, Java</skills></person>”;
String name=request.getParameter(“empName”);
String age=request.getParameter(“age”);
String skills=request.getParameter(“skills”);
String newEmployee =”<person><name>” + name+ “</name><age>” + age + “</age><skills>”+ skills +”</skills></person>”;
if (name == null){
out.println(employees+”</people>”);
}else{
out.println(employees + newEmployee + “</people>”);
}
%>
Fiz o exemplo e acontece esse erro:
[RPC Fault faultString="HTTP request error" faultCode="Server.Error.Request" faultDetail="Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: employee.jsp"]. URL: employee.jsp”]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()
at mx.rpc::Responder/fault()
at mx.rpc::AsyncRequest/fault()
at ::DirectHTTPMessageResponder/errorHandler()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/flash.net:URLLoader::redirectEvent()
Poderia me ajudar!
[]´s
Sry, I didnt see the language!!
Some can help me???
hi here i have problem
i want to assign value in string variable but it will not work my code is below (jsp code)
<%
String as=”";
out.println(as);
%>
it will display nothing have an idea about that please help me.
hi here i have problem
i want to assign value in string variable but it will not work my code is below (jsp code)
<%
String as=”";
out.println(as);
%>
it will display nothing have an idea about that please help me.
hi here i have problem
i want to assign value in string variable but it will not work my code is below (jsp code)
<%
String as=””;
out.println(as);
%>
it will display nothing have an idea about that please help me.
i want complete coding for employee skill database in jsp as a front end and mysql as back end.
hi can u send me the step by step procedure if placing the folders while doing in flex development…
hi
this is excellent tutorial..it helped me somuch.
even i m getting the same error specified by alexandre. Krishna were u able to run the program successfully.
actually i mlloking for a code which can post data from flex form to jsp.
Hello, It is super blogs, enjoy my seo
I had fun understanding this post. I want to see more on this subject.. Thanks for writing this good article.. Anyway, Im going to subscribe to your rss and I wish you write great articles again soon.
i want 10 sample prog for connecting jsp with flex
it is nice but i want some sample programs for connecting jsp with flex.
Hi There,
This is nice tetorial. But I stuck one place.
I have Eclipse for creating web project. Now my JSP file is created in eclispse enviroment.
Some where. C:\myapplication\storefront\TractionAutiomation\WebContent\FlexTest.jsp
I have Flex Builder for creating Flex UI,
Location is : C:\Documents and Settings\Administrator\My Documents\Flex Builder 3\FlexDemo\src\FlexDemo.mxml
What will be URI for my JSP file or where should I put my JSP file.
Please Advice.
I need to start any server for executing this code?
Please help me.
and provide me steps.
Thanks in advance.
Thanks and Best Regards,
Jamil