473,218 Members | 1,423 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,218 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 18964
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. ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.