Hi,
I have a basic web page that makes an ajax call to a servlet to delete a file on the server. The code below successfully deletes the file, however there seems to be some problem with the response it's sending. On a successful deletion the file should disappear from the list of files on the web page, but it doesn't until I refresh the page.
I'm using Tomcat 6.20 and get the following message in my console:
2010-01-04 10:34:06,125: [http-8080-1] DEBUG AttachmentDAO - getting list of Attachments with Notice ids: 314
Hibernate: select this_.attachmentid as attachme1_0_0_, this_.noticeid as noticeid0_0_, this_.attach_name as attach3_0_0_, this_.attach_path as attach4_0_0_, this_.attach_mime as attach5_0_0_, this_.attach_size as attach6_0_0_ from attachment this_ where this_.noticeid=? order by this_.attach_name asc
Jan 4, 2010 10:34:12 AM org.apache.tomcat.util.http.Parameters processParameters
WARNING: Parameters: Invalid chunk ignored.
Deleting attachment #494
2010-01-04 10:34:12,468: [http-8080-1] DEBUG AttachmentDAO - getting Attachment instance with id: 494
Hibernate: select attachment0_.attachmentid as attachme1_0_0_, attachment0_.noticeid as noticeid0_0_, attachment0_.attach_name as attach3_0_0_, attachment0_.attach_path as attach4_0_0_, attachment0_.attach_mime as attach5_0_0_, attachment0_.attach_size as attach6_0_0_ from attachment attachment0_ where attachment0_.attachmentid=?
Hibernate: delete from attachment where attachmentid=?
D:/Dev/Proj/drpweb_upload_testing/upload/314/help_icon.jpg
Here is the method from my servlet:
public void deleteNoticeAttachment(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String attachId;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
attachId = request.getParameter("attachmentid");
if (attachId == null) {
System.err.append("Attachment ID is null");
return;
}
try {
System.out.println("Deleting attachment #" + attachId);
//Delete the database reference.
AttachmentDAO attachDAO = new AttachmentDAO();
Attachment attach = attachDAO.findById(Integer.parseInt(attachId));
if (attach != null){
attachDAO.delete(attach);
//Delete the actual file.
//MCF: changed url for local testing.
//String fileUrl = "/var/www/calgwebe/cominclude/upload/" + attach.getNoticeid() + "/"
// + attachment.getString("attach_name");
String fileUrl = "D:/Dev/Proj/drpweb_upload_testing/upload/" + attach.getNoticeid() + "/"
+ attach.getAttachName();
File file = new File(fileUrl);
System.out.println(fileUrl);
if (file.exists()) {
file.delete();
}
}
out.write("SUCCESS");
} catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
e.printStackTrace();
out.write("FAILED");
} finally {
if (out != null){
out.close();
}
}
}
Any ideas why my response is not being received from my servlet?
Here is the JS in case you need it. makeRequest is first called and in turn calls alertContents.
function makeRequest(params, url) {
http_request = false;
var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-Type", contentType);
http_request.send(params);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
if(http_request.responseText == "FAILED") {
alert("Attachment removal failed. You can try again later, or re-create the notice without the attachment.");
}
else {
var attachment = getE("attachment_" + http_request.responseText);
attachment.parentNode.removeChild(attachment);
}
} else {
alert('There was a problem with the request.');
}
}
}