基于HTTPService方式给后台传递参数的方法(一)

时间:2022-10-31 21:29:10

定义的Servlet的结构:

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  System.out.println(request.getRequestURL());
  
  String id = request.getParameter("uid");
  System.out.println(id);
  
  PrintWriter out = response.getWriter();
  String temp = "<node><name>liusong</name><age>1</age></node>"
   +"<node><name>kobe</name><age>2</age></node>"
   +"<node><name>jordan</name><age>3</age></node>";
  System.out.println(temp);
  out.write(temp);
  
 }

 

flex里面mxml的内容:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">

 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.rpc.events.FaultEvent;
   import mx.rpc.events.ResultEvent;

   //trace the result of the service out
   private function serviceResult(event:Event):void
   {
    Alert.show(service.lastResult.node);
   }

   // in the event that the service faults or times out
   private function serviceFault(event:Event):void
   {
    Alert.show('broken service');
   }

   private function callService():void
   {
    var url1:String = "http://localhost:8080/aaa/servlet/myservlet";
    url1 +="?uid=" + input.text;
    service.url = url1;
    service.send();
   }
  ]]>
 </mx:Script>
 
 <mx:HTTPService  id="service" result="serviceResult(event)" fault="serviceFault(event)" method="POST" contentType="application/xml" useProxy="false" showBusyCursor="true">

 </mx:HTTPService>
 <mx:TextInput id="input"/>
 <mx:Button label="get user name" click="callService()"/>
 <mx:DataGrid dataProvider="{service.lastResult.node}">
  <mx:columns>
   <mx:DataGridColumn dataField="name" headerText="姓名" />
   <mx:DataGridColumn dataField="age" headerText="年龄"/>
  </mx:columns>
 </mx:DataGrid>
</mx:Application>

 

 

注意必要jar文件的导入!!!!!!!