【Flex4】中httpservice与服务器端交互的值传递

flex中的代码:

<!--定义HttpService发送请求-->  
<s:HTTPService id="service"   
               url="http://localhost:8080/testhttpservice/testHttpServiceServlet"   
               useProxy="false"   
               fault="service_faultHandler(event)"   
               result="service_resultHandler(event)">  
    <!--第一种传值方式-->  
      
    <s:request >  
        <!--参数名称作标签,中间填充参数值-->  
        <username>{txtusername.text}</username>  
        <password>{txtpassword.text}</password>  
    </s:request>  
      
</s:HTTPService>

Servlet在web.xml中定义

<servlet>  
  <servlet-name>UserServlet</servlet-name>   
  <servlet-class>cn.servlet.UserServlet</servlet-class>   
</servlet>  
<servlet-mapping>  
  <servlet-name>UserServlet</servlet-name>   
  <url-pattern>/UserServlet</url-pattern>   
</servlet-mapping>

Servlet内容

package cn.servlet;  
  
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 UserServlet extends HttpServlet {  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        System.out.println("***********  进入Services方法中 ***********");  
          
                request.setCharacterEncoding("utf-8");    
                response.setCharacterEncoding("utf-8");    
          
                String userName=request.getParameter("username");    
                String passWord=request.getParameter("password");    
          
        PrintWriter out = response.getWriter();  
        out.println("用户名:"+ userName +",密码:"+ passWord);  
        out.flush();  
        out.close();  
    }  
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        doGet(request, response);  
    }  
      
}

Flex应用程序

<?xml version="1.0" encoding="utf-8"?>  
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
               xmlns:s="library://ns.adobe.com/flex/spark"   
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">  
    <fx:Script>  
        <![CDATA[ 
            import mx.controls.Alert; 
            import mx.rpc.events.FaultEvent; 
            import mx.rpc.events.ResultEvent; 
             
            protected function http_result(event:ResultEvent):void { 
                Alert.show("成功了:"+event.result as String,"提示");  
            } 
             
            protected function http_fault(event:FaultEvent):void { 
                trace(event.message); 
                Alert.show("失败了");  
            } 
             
            protected function btn_click(event:MouseEvent):void { 
                //第一种传值方式                  
                service.send();  
                 
                //第二种传值方式 通过send()方法传值 send方法中传递参数   
                //定义一object对象  
//              var obj:Object=new Object();  
//              //分别将文本框username,password的值传递到后台 ,object对象.参数名=值   传值操作  
//              obj.username = txt_name.text;  
//              obj.password = txt_pwd.text;  
//              service.send(obj);  
            } 
             
        ]]>  
    </fx:Script>  
    <fx:Declarations>  
        <!--定义HttpService发送请求-->    
        <s:HTTPService id="service" url="UserServlet" method="POST" result="http_result(event)" fault="http_fault(event)">  
            <!--第一种传值方式-->    
            <s:request>    
                <!--参数名称作标签,中间填充参数值-->    
                <username>{txt_name.text}</username>    
                <password>{txt_pwd.text}</password>    
            </s:request>    
        </s:HTTPService>  
    </fx:Declarations>  
      
    <s:TextInput x="332" y="196" id="txt_name"/>    
    <s:TextInput x="332" y="227" id="txt_pwd" displayAsPassword="true"/>    
    <s:Label x="286" y="201" text="用户名:" />    
    <s:Label x="298" y="233" text="密码:" />    
    <s:Button x="359" y="257" label="发送" click="btn_click(event)"/>    
</s:Application>

Flex 中 httpservice 与服务器端交互的值传递


未经允许请勿转载:程序喵 » 【Flex4】中httpservice与服务器端交互的值传递

点  赞 (0) 打  赏
分享到: