FLEX httpService 用法介绍

£可爱£侵袭症+ 提交于 2020-02-05 02:18:20

传递请求到Java后台自己总结出了三种方式,也是我在项目当中用到的三种方式,今天把它写出来,一是为正在学习flex的新手一个好的指导,二是给自己是一个不忘的笔记:
一,通过httpService向java的servlet发送请求,然后返回一个结果:
 第一步新一个httpService.mxml文件:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Button x="277" y="162" label="点击" width="194" height="73" id="myButton" click="myFunction()"/>
 
 <mx:HTTPService
   id="myHttpService"
   result="myResult(event)"
   fault="myFault(event)"
   method="POST"
   url="http://127.0.0.1:8080/httpService01/httpservcie.do">
  <mx:request xmlns="">
   <userName>何正元</userName>
   <userPwd>123</userPwd>
  </mx:request>
 </mx:HTTPService>
 
 <mx:Script>
  <![CDATA[
   import mx.rpc.events.FaultEvent;
   import mx.controls.Alert;
   import mx.rpc.events.ResultEvent;
   private function myFunction():void{
    myHttpService.send();
   }
   private function myResult(event:ResultEvent){
    var msg = event.result.toString();
    Alert.show(msg);
   }
   private function myFault(event:FaultEvent){
    var msg = event.fault.toString();
    Alert.show("错误:"+msg);
   }
  ]]>
 </mx:Script>
</mx:Application>
 第二步,写一个servlet:
 package com.lovo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpService extends HttpServlet {

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


  this.doPost(request, response);
 }

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

  request.setCharacterEncoding("UTF-8");
  response.setContentType("text/html;charset=utf-8;");
  PrintWriter out = response.getWriter();
 
  String userName = request.getParameter("userName");
  
  String userPwd = request.getParameter("userPwd");
  out.print("你输入的姓名为:"+userName+ "密码为:"+userPwd);
  
 }

}
对于httpServlet就介绍完成了。上面代码是通过测试的。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!