본문 바로가기

카테고리 없음

HTTPService Parameters

HTTPService 콤포넌트를 이용하여 웹서버에 요청할 때 파라미터를 전달하는 다양한 방법

MXML

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="359" width="480">
 <mx:Script>
  <![CDATA[
   import mx.rpc.events.ResultEvent;
   import mx.rpc.events.FaultEvent;
   import mx.controls.Alert;
         [Bindable]
         private var employeeInfo:XMLList;
         public function handleXML(event:ResultEvent):void
         {
            employeeInfo = event.result.emp as XMLList;
         }
         public function handleFault(event:FaultEvent):void
         {
            Alert.show(event.fault.faultString, "Error");
         }
        
         public function getDetailedInfo1():void {
          var http:HTTPService = new HTTPService();
          http.method="POST";
          http.url = "http://localhost/flex/xmlEmpService4.jsp";
          http.resultFormat = "e4x";
          http.request.empno = txtEmpno.text;
          http.addEventListener("result", handleXML);
          http.addEventListener("fault", handleFault);
          http.send();
         }
        
         public function getDetailedInfo2():void {
           var params:Object = new Object();
           params.empno = txtEmpno.text;
           xmlRPC.send(params);
         }
        
         public function getDetailedInfo3():void {
           xmlRPC.url="http://localhost/flex/xmlEmpService4.jsp?empno="+txtEmpno.text;
           xmlRPC.send();
         }
        
         public function getDetailedInfo4():void {
           var params:Object = new Object();
           params["empno"] = txtEmpno.text;
           xmlRPC.send(params);
         }

         public function getDetailedInfo5():void {
           xmlRPC.request.empno = txtEmpno.text;
           xmlRPC.send();
         }
        public function clearDataGrid():void {
          employeeInfo = null;
        }
  ]]>
 </mx:Script>
    <mx:HTTPService result="handleXML(event);" fault="handleFault(event);" id="xmlRPC" resultFormat="e4x"
         method="POST" url="http://localhost/flex/xmlEmpService4.jsp" useProxy="false">
     <mx:request xmlns="">
      <empno>{ txtEmpno.text }</empno>
     </mx:request>
 </mx:HTTPService>
 
  <mx:Label x="49.5" y="39" text="HTTPService를 이용하여 파라미터를 전달하는 방법 테스트" fontSize="13" fontWeight="bold" textDecoration="underline"/>
  <mx:Label x="43" y="85" text="사번(empno)입력" fontSize="12"/>
  <mx:TextInput x="157" y="83" width="88" id="txtEmpno" text="7839"/>
 
  <mx:DataGrid x="43" y="113" height="142" id="EmpList" dataProvider="{employeeInfo}" fontSize="12" textAlign="center">
   <mx:columns>
    <mx:DataGridColumn headerText="사 번" dataField="empno"/>
    <mx:DataGridColumn headerText="이 름" dataField="ename"/>
   </mx:columns>
  </mx:DataGrid>
 
  <mx:Button x="253" y="83" label="&lt;empno&gt;7839&lt;/empno&gt;" click="xmlRPC.send();" width="187" height="22"/>
  <mx:Button x="253" y="113" label="http.request.empno='7839'" width="187" id="btnDetail"
  click="getDetailedInfo1();"/>  
  <mx:Button x="253" y="143" label="params.empno='7839'" width="187" id="btnDetail0"
   click="getDetailedInfo2();"/>
  <mx:Button x="253" y="173" label="emp.jsp?empno=7839" width="187" id="btnDetail1"
   click="getDetailedInfo3();"/>
  <mx:Button x="253" y="203" label="params['empno']='7839'" width="187" id="btnDetail2"
   click="getDetailedInfo4();"/>
  <mx:Button x="253" y="208" label="http.request.empno='7839'" width="187" id="btnDetail5"
   click="getDetailedInfo5();"/>
  <mx:Button x="253" y="233" label="Clear DataGrid" width="187" id="btnDetail3"
   click="clearDataGrid();" fillAlphas="[1.0, 1.0]" fillColors="[#F8BA35, #F8BA35]"/>
</mx:Application>



테스트용 서버측 프로그램 (xmlEmpService04.jsp)

<%@ page contentType="text/xml;charset=utf-8" import="java.sql.*" %>
<%
 Connection conn = null;
 Statement stmt = null;

 String jdbc_driver = "oracle.jdbc.OracleDriver";
 String db_url = "jdbc:oracle:thin:@localhost:1521:ora9i";
 String empno = request.getParameter("empno");

 try{
  Class.forName(jdbc_driver);

  conn = DriverManager.getConnection(db_url,"scott","tiger");

  stmt = conn.createStatement();

  ResultSet rs = stmt.executeQuery("select * from emp where empno="+empno);
 // EMPNO  ENAME  JOB  MGR  HIREDATE  SAL  COMM  DEPTNO        
  %>
  <?xml version="1.0" encoding="utf-8"?>
  <employee>

  <%
  while(rs.next()) { %>
  <emp>
    <empno><%=rs.getInt(1)%></empno>
    <ename><%=rs.getString(2)%></ename>
    <job><%=rs.getString(3)%></job>
    <mgr><%=rs.getString(4)%></mgr>
    <hiredate><%=rs.getString(5)%></hiredate>
    <sal><%=rs.getInt(6)%></sal>
    <comm><%=rs.getString(7)%></comm>
    <deptno><%=rs.getInt(8)%></deptno>
</emp>
<%  } %>
</employee>
<%
  rs.close();
  stmt.close();
  conn.close();
 }
 catch(Exception e) {
  out.println(e);
 }
%>