By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

Problem with XMLHTTP responseText empty

P: 3
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
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
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

P: 3
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
Expert Mod 15k+
P: 16,027
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

Post your reply

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