Problem with XMLHTTP responseText empty

J

JaneneM

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:

*******

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">

var httpRequest;

function doUpload( )
{

url = "http://localhost:8022/FileUploadWithHttp/FileUpload.do";

httpRequest=null;
try {
httpRequest=new XMLHttpRequest();
}
// code for IE
catch (e) {
try {
httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support Ajax.");
return false;
}
}
if (httpRequest!=null) {
try {
httpRequest.open('POST',url,true);
} catch (e) {
alert(e);
}
httpRequest.onreadystatechange=state_Change;
httpRequest.send(null);
return true;
}
else
{
alert("Your browser does not support XMLHTTP.");
return false;
}
}
function state_Change()
{
// if xmlhttp shows "complete"
if (httpRequest.readyState==4) {
if (httpRequest.status == 200) {
contents = httpRequest.responseText; // ALWAYS BLANK!!! ARG
alert(contents);
}
}
}

</script>

<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Select File</title>
</head>
<body>

<form name="myform" method="post" action="http://localhost:8022/
FileUploadWithHttp/FileUpload.do"
enctype="multipart/form-data" onsubmit="javascript:doUpload();"
id="myform" target="fileUploadResultsFrame">
Select your file:<br />
<input type="file" name="myfile"><br /><br />
<input type="submit" name="Submit" value="Submit your file"/>
</form>
File Contents: <br>
<iframe id="fileUploadResultsFrame" src="FileUploadResults.html" ;
scrolling="no"
frameborder="1" height="100" width="50%" longdesc="File Upload
Results"
name="fileUploadResultsFrame" /> </iframe>

</body>
</html>

*******

Here's the little servlet I'm running via Tomcat:

package org.natureserve.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;

public class FileUploadServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{

// first check if the upload request coming in is a multipart
request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);

// // if not, discard - DO TO something more meaningful
if (!isMultipart) {
//request.setAttribute("msg", "ERROR: Request was not multipart!");
//request.getRequestDispatcher("msg.jsp").forward(request,
response);
return;
}
// // Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
//
// // Parse the request
try {
FileItemIterator items = upload.getItemIterator(request);
if (items.hasNext()) {
FileItemStream item = (FileItemStream)items.next();
// check if the current item is a form field or an uploaded file
if (item.isFormField()) {
// skip
} else {
String type = item.getContentType();
if (!type.contains("csv") && !type.contains("excel")
&& !type.contains("comma")) {
request.setAttribute("msg",
"ERROR: Request was not csv!");
request.getRequestDispatcher("msg.jsp").forward(
request, response);
return;
}
String name = item.getFieldName();
BufferedReader stream = new BufferedReader((new
InputStreamReader(
item.openStream())));

response.setContentType("text/html; charset=ISO-8859-1");
String contents = stream.readLine();
PrintWriter out = response.getWriter();
out.print(contents);
out.flush();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}

******

And msg.jsp is just this:

<%

String msg = (String)request.getAttribute("msg");

if(msg != null) {
if (msg.contains("ERROR")) {
out.println("<font size=+1>" + msg + "</font><br/>");
out.println("Use back button to select a different file.");
return;
}
}
%>


*****

The iframe file is just a blank html file - it seems to be overwritten
anyway by the results of the request.
 
M

Martin Honnen

JaneneM said:
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.

The browser will make two requests, one for the form submission, one for
the open/send sequence of the XMLHttpRequest. If responseText is empty
then the server does not send any response body for the request of the
XMLHttpRequest object. What you see in the iframe is the response to the
form submission, if _that_ response is not empty is not relevant, as the
response to the XMLHttpRequest is a completely different response in
terms of HTTP. However if you want to update a field in your main
document then you could simply do that by inserting script in the HTML
document sent as the result of the form submission to the iframe e.g.
if (parent != window) {
parent.document.forms.formName.elements.fieldName.value = '...';
}
httpRequest.open('POST',url,true);

So you do a HTTP POST request here with no particular HTTP response
headers set, and
} catch (e) {
alert(e);
}
httpRequest.onreadystatechange=state_Change;
httpRequest.send(null);

you do not even send a response body for the HTTP POST.
I suppose your servlet is simply not doing anything meaningful with that
request, maybe because of this:
// first check if the upload request coming in is a multipart
request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);

// // if not, discard - DO TO something more meaningful
if (!isMultipart) {
//request.setAttribute("msg", "ERROR: Request was not multipart!");
//request.getRequestDispatcher("msg.jsp").forward(request,
response);
return;
}

That is partly a guess, I am not really familiar with servlet APIs and
they are off topic here. But your assumption seems to be flawed, there
are two requests and two responses why you seem to assume there is only
one response.
 
J

JaneneM

there are two requests and two responses

Ok. I was beginning to suspect this.

I'll look into catching the form submit response, thanks.

Janene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top