473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with XMLHTTP responseText empty

I've implemented a FileUpload servlet using AJAX and JS. It appears to
be working well but for one issue. I used XMLHTTP so I could intercept
the response in Javascript and write it out to a field on my webpage.
I get back that my readyState is 4 and my status is 200 and status
text is "OK", but my responseText is always empty. Both on FireFox and
IE7, so it must be something I'm doing (wrong). I've also set up an
IFRAME to redirect the results of the POST, and that updates fine, so
the response *is* coming back, but just not the
xmlhttpRequest.responseText. I'm baffled.

Any ideas? TIA!

Janene

Here's my HTML file:

*******
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
  2. www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <script type="text/javascript">
Expand|Select|Wrap|Line Numbers
  1. var httpRequest;
  2.  
  3. function doUpload(  )
  4. {
  5.  
  6.         url = "http://localhost:8022/FileUploadWithHttp/FileUpload.do";
  7.  
  8.         httpRequest=null;
  9.         try {
  10.                 httpRequest=new XMLHttpRequest();
  11.         }
  12.         // code for IE
  13.         catch (e) {
  14.                 try {
  15.                         httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
  16.          }
  17.          catch (e) {
  18.                 alert("Your browser does not support Ajax.");
  19.                         return false;
  20.                 }
  21.         }
  22.         if (httpRequest!=null) {
  23.                 try {
  24.                         httpRequest.open('POST',url,true);
  25.                 } catch (e) {
  26.                         alert(e);
  27.                 }
  28.                 httpRequest.onreadystatechange=state_Change;
  29.                 httpRequest.send(null);
  30.                 return true;
  31.     }
  32.     else
  33.     {
  34.                   alert("Your browser does not support XMLHTTP.");
  35.                   return false;
  36.         }
  37. }
  38.  
  39. function state_Change()
  40. {
  41.         // if xmlhttp shows "complete"
  42.         if (httpRequest.readyState==4) {
  43.                 if (httpRequest.status == 200) {
  44.                   contents = httpRequest.responseText; // ALWAYS BLANK!!! ARG
  45.                   alert(contents);
  46.                 }
  47.         }
  48.  
  49. }
Expand|Select|Wrap|Line Numbers
  1. </script>
  2.  
  3. <meta http-equiv="Content-Type" content="text/html;
  4. charset=ISO-8859-1">
  5. <title>Select File</title>
  6. </head>
  7. <body>
  8.  
  9. <form name="myform" method="post" action="http://localhost:8022/
  10. FileUploadWithHttp/FileUpload.do"
  11.                 enctype="multipart/form-data" onsubmit="javascript:doUpload();"
  12. id="myform" target="fileUploadResultsFrame">
  13.     Select your file:<br />
  14.       <input type="file" name="myfile"><br /><br />
  15.       <input type="submit" name="Submit" value="Submit your file"/>
  16.     </form>
  17.     File Contents: <br>
  18.         <iframe id="fileUploadResultsFrame" src="FileUploadResults.html" ;
  19. scrolling="no"
  20.                 frameborder="1" height="100" width="50%" longdesc="File Upload
  21. Results"
  22.                 name="fileUploadResultsFrame" /> </iframe>
  23.  
  24. </body>
  25. </html>
  26.  
*******

Here's the little servlet I'm running via Tomcat:
Expand|Select|Wrap|Line Numbers
  1. package org.natureserve.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.util.Iterator;
  8. import java.util.List;
  9.  
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import org.apache.commons.fileupload.*;
  15. import org.apache.commons.fileupload.servlet.*;
  16.  
  17. public class FileUploadServlet extends HttpServlet {
  18.  
  19.                 @Override
  20.         protected void doPost(HttpServletRequest request,
  21.                         HttpServletResponse response) throws ServletException, IOException
  22. {
  23.  
  24.                 // first check if the upload request coming in is a multipart
  25. request
  26.                 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  27.  
  28. //              // if not, discard - DO TO something more meaningful
  29.                 if (!isMultipart) {
  30.                         //request.setAttribute("msg", "ERROR: Request was not multipart!");
  31.                         //request.getRequestDispatcher("msg.jsp").forward(request,
  32. response);
  33.                         return;
  34.                 }
  35. //              // Create a new file upload handler
  36.                 ServletFileUpload upload = new ServletFileUpload();
  37. //
  38. //              // Parse the request
  39.                 try {
  40.                         FileItemIterator items = upload.getItemIterator(request);
  41.                         if (items.hasNext()) {
  42.                                 FileItemStream item = (FileItemStream)items.next();
  43.                                 // check if the current item is a form field or an uploaded file
  44.                                 if (item.isFormField()) {
  45. // skip
  46.                                 } else {
  47.                                         String type = item.getContentType();
  48.                                         if (!type.contains("csv") && !type.contains("excel")
  49.                                                         && !type.contains("comma")) {
  50.                                                 request.setAttribute("msg",
  51.                                                                 "ERROR: Request was not csv!");
  52.                                                 request.getRequestDispatcher("msg.jsp").forward(
  53.                                                                 request, response);
  54.                                                 return;
  55.                                         }
  56.                                         String name = item.getFieldName();
  57.                                         BufferedReader stream = new BufferedReader((new
  58. InputStreamReader(
  59.                                                         item.openStream())));
  60.  
  61.                                         response.setContentType("text/html; charset=ISO-8859-1");
  62.                                         String contents = stream.readLine();
  63.                                         PrintWriter out = response.getWriter();
  64.                                         out.print(contents);
  65.                                         out.flush();
  66.                                 }
  67.                         }
  68.                 } catch (FileUploadException e) {
  69.                         e.printStackTrace();
  70.                 }
  71.         }
  72.  
  73. }
  74.  
******

And msg.jsp is just this:
Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3.   String msg = (String)request.getAttribute("msg");
  4.  
  5.   if(msg != null) {
  6.           if (msg.contains("ERROR")) {
  7.                 out.println("<font size=+1>" + msg + "</font><br/>");
  8.                 out.println("Use back button to select a different file.");
  9.                 return;
  10.           }
  11.   }
  12. %>
*****

The iframe file is just a blank html file - it seems to be overwritten
anyway by the results of the request.
Sep 5 '07 #1
3 18987
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Thanks for using code tags. You can specify a language too for the code tag, e.g. [code=javascript].

What output would you get if you were to run the servlet normally?
Sep 6 '07 #2
Welcome to TSDN!

Thanks for using code tags. You can specify a language too for the code tag, e.g. [code=javascript].
Oooh, pretty! Thanks!

What output would you get if you were to run the servlet normally?
The goal of this servlet dance is to read a csv file from the client's computer and fill in a textfield with the contents. For now, if it works, the servlet will just return the text read from the client's file. My test file has just "cat, bat" in it, so that is all that appears when I run it without all the fancy stuff.

Thanks,

Janene
Sep 6 '07 #3
acoder
16,027 Expert Mod 8TB
With a POST, you have to send data using the send method. See this example.

Also, uploading via XMLHttpRequest Ajax is not really possible. You have to use an iframe - see this thread.
Sep 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: fochie | last post by:
Greetings, I'm having a problem when I try to GET a file from my server via xmlhttp when using Mozilla. With IE I can get any type of file fine, get/display headers fine, etc. With Mozilla,...
1
by: KoosJaspers | last post by:
I have a remarkable problem. Opening a file using xmlhttp works perfectly. The responseText output is read, since it can be assigned to an alert() message, as follows : alert(xmlhttp.resposeText)...
5
by: marcus6 | last post by:
Howto access xmlHttp.responseText as document.getElementByTagName ? If I access a .html page through the xmlHttp, how can I use the functions .getElementsByTagName and .getElementById on the data...
17
by: eros | last post by:
During development (debugging) the site works well but when I release from the debugging and test into production.. All the AJAX was not performed.. the result from xmlHttp.responseText was not...
1
by: farghal | last post by:
Hello as many people I'm new to ajax but trying my best to understand. At this point I got a problem I'm not able to solve. I've looked on several forums and googled internet but I can't find a...
21
vikas251074
by: vikas251074 | last post by:
I am getting error while entry in userid field. When user enter his user id, an event is fired immediately and user id is verified using AJAX method. But I am getting error 'Object doesn't support...
2
by: trpost | last post by:
Is it possible to execute javascript as passed in xmlHttp.responseText Here is what I am doing: search.js var xmlHttp xmlHttp=GetXmlHttpObject() var url="search.php"...
1
by: StevenS | last post by:
Ok, I'm very new to AJAX programming, and fairly new to Javascript. (I was originally trained on low-level C programming.) I'm trying to build a simple AJAX routine in a file named ajax.js: ...
1
by: uhdam | last post by:
HI... In netbeans, whenever i launch the app it loads up and then i get an empty pane where the gui would be. Initially i used Netbeans 6.5. Later i used Netbeans 5.0 and had the prob. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.