0

我能够从 as3 获取数据到 servlet 成功读取它,但无法从 servlet 发送回 as3。

下面是 AS3 代码。应用程序加载到 Tomcat 服务器上。

var request:URLRequest = new URLRequest("http://localhost:8080/Demo/UserDataControllerServlet");

var loader:URLLoader = new URLLoader();

 loader.dataFormat = URLLoaderDataFormat.VARIABLES;

  add_txt.text ="http://localhost:8080/Demo/UserDataControllerServlet";

  var _vars:URLVariables = new URLVariables();

  _vars.emailId = "abc@gmail.com";
  _vars.phoneNumber = "9898989898";
  _vars.userAddress = "abc";
  _vars.userName = "abc";
  _vars.vendorAddress = "abc";
  _vars.vendorName = "abc";

  request.data = _vars;

  request.method = URLRequestMethod.POST;

  loader.addEventListener(Event.COMPLETE, handleComplete);

  loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
  loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
  loader.load(request);
  add_txt.text ="already sent........................";
  private function onSecurityError(event:SecurityErrorEvent):void {
  add_txt.text ="security error ........................";
  }

这应该在响应返回到 as3 后执行。

private function handleComplete(event:Event):void {

add_txt.text ="in the response function";                
var loader:URLLoader = URLLoader(event.target);             
trace("Message: " + loader.data);
add_txt.text = "========="+loader.data;             
}
private function onIOError(event:IOErrorEvent):void {
add_txt.text = "Error loading URL.";
trace("Error loading URL.");
this.parent.removeChild(this);
}

下面是 servlet 代码: 我能够从请求中读取数据,但是在 out.write 和 out.close 之后响应不会返回到 as3。我尝试删除 out.close() 但即使这样也无济于事。

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String emailId  = request.getParameter("emailId");       
        String phoneNumber = request.getParameter("phoneNumber");
        String userAddress = request.getParameter("userAddress");
        String userName = request.getParameter("userName");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.write("Hello");
        out.close();
        }       

你能帮我解决这个在 as3 中没有接收到数据的问题吗?

4

2 回答 2

0

你已经成功了一半。您需要将对 servlet 的调用视为 Web 服务调用。将您视为向 servlet 发出 Ajax 请求。在“REST Servlets”上查找内容。 http://www.mkyong.com/webservices/jax-rs/resteasy-hello-world-example/

于 2013-02-05T17:58:22.780 回答
0

我认为您应该以查询字符串格式向 AS3 返回响应,如下所示:

private function handleComplete(event:Event):void {

         add_txt.text ="in the response function";                 
         var loader:URLLoader = URLLoader(event.target);             
         trace("Message: " + loader.data.success); // loading [success] value from response
         add_txt.text = "========="+loader.data.success;             
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String emailId  = request.getParameter("emailId");       
        String phoneNumber = request.getParameter("phoneNumber");
        String userAddress = request.getParameter("userAddress");
        String userName = request.getParameter("userName");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.write("success=1");  // sending [success]
        out.close();
}       
于 2013-02-05T19:45:02.190 回答