我能够从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数据的问题吗?
答案 0 :(得分:0)
你在那里一半。您需要将对servlet的调用视为Web服务调用。您可以将AS视为向servlet发出Ajax请求。查看“REST Servlets”上的内容。 http://www.mkyong.com/webservices/jax-rs/resteasy-hello-world-example/
答案 1 :(得分: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();
}