如何创建一个flex文件,将文本输入传递给servlet并重定向到同一个servlet

时间:2012-10-19 06:40:04

标签: java flex servlets

我想将mxml文件重定向到servlet并在servlet上打印代码但是当我使用导航到url代码然后输出显示servlet页面中textinput字段的 null 值时,请帮帮我为了解决问题,下面给出了mxml和servlet的基本编码

MXML文件

<s:Application...>
<fx:Script>
<![CDATA[
  import flash.net.navigateToURL;
  import mx.controls.*;
  import mx.events.Request;
  import mx.rpc.events.FaultEvent;
  import mx.rpc.events.ResultEvent;

  public function resultHandler(event : ResultEvent):void
  {             
    navigateToURL(new URLRequest('Srvlt'),'_self')
  }
  public function faultHandler(event : FaultEvent):void
  {
    Alert.show("error");
  }
]]>
</fx:Script>

<fx:Declarations>
  <s:HTTPService id="service" url="Srvlt" result="resultHandler(event)" fault="faultHandler(event)" method="GET" showBusyCursor="true" resultFormat="text">
    <s:request xmlns="">
      <email>{email.text}</email>
      <pass>{pass.text}</pass>
    </s:request>
  </s:HTTPService>
</fx:Declarations>
<s:VGroup verticalAlign="middle">
  <s:FormItem label="E-mail : ">
    <s:TextInput id="email" />
  </s:FormItem>
  <s:FormItem label="Password :">
    <s:TextInput id="pass" displayAsPassword="true" />
  </s:FormItem>
  <mx:Button label="Submit" click="service.send()" />
</s:VGroup>
</s:Application>

servlet的get方法编码如下

String e = request.getParameter("email");
String p = request.getParameter("pass");
String resul;
PrintWriter out = response.getWriter();
resul = "Hi i am servlet, your username is " + e + " & password is " + p + ". are they correct ?";
out.print(resul);

1 个答案:

答案 0 :(得分:0)

您在应用程序中发送了2个不同的请求。第一个是HTTPService,第二个是URLRequest

<s:HTTPService id="service" url="Srvlt" result="resultHandler(event)"
fault="faultHandler(event)" method="GET" showBusyCursor="true" resultFormat="text">
    <s:request xmlns="">
        <email>{email.text}</email>
        <pass>{pass.text}</pass>
    </s:request>
</s:HTTPService>

处理结果时,您可以通过ResultEvent对象

获取响应字符串
public function resultHandler(event : ResultEvent):void
{
    Alert.show(event.result); // you'll see "Hi i am servlet..." here
}

您现在可以使用此字符串。

但是如果要打开包含结果文本的新页面,则不需要使用HTTPService。使用URLRequest类。

var request:URLRequest = new URLRequest("Srvlt");
var variables:URLVariables = new URLVariables();
variables.email = email.text;
variables.pass = pass.text;
request.data = variables;
navigateToURL(request);

所以,你现在做错了。不要发送2个请求。选择HTTPService并在应用程序内处理结果,或navigateToURLURLRequest,如果您只想在新窗口中显示响应。