Reputation: 1588
I am getting an error in my java servlet. The servlet fills the fields of an existing pdf. The page pulls up an alert asking "Do you want to open or save this file?" So if I press "Open" or "Save" it works fine, and does the correct thing. But.... if I press "Cancel", nothing pops up, and I get this error:
com.evermind.server.http.HttpIOException: An existing connection was forcibly closed by the remote host
I am not sure why or where this error is coming about, but it says that the error is happening on this line:
stamp.close();
If more code snippets are needed, please let me know. I just didnt want to paste everything in here, because I dont exactly know where it is happening. Thanks in advance.
EDIT
Here is the majority of my relevant code:
try {
conn = ((DataSource) new InitialContext().lookup(dSource)).getConnection();
stmt = conn.prepareStatement("....");
rs = stmt.executeQuery();
if (rs.next()) {
....
}
stmt = conn.prepareStatement("....");
rs = stmt.executeQuery();
if (rs.next()) {
....
}
if (isTempVerification) {
final String tempFile = "TemporaryVerification.pdf";
try {
response.setHeader(contentDisposition, "attachment; filename=" + tempFile);
reader = new PdfReader(this.getServletContext().getResource("/pdf/" + tempFile));
stamp = new PdfStamper(reader, response.getOutputStream());
form = stamp.getAcroFields();
form.setField("date", current);
form.setField("reply_line", replyLine);
form.setField("first_middle_last", fmlName);
form.setField("term_year_1", termYear + ".");
form.setField("census_date", termCensus);
form.setField("term_year_2", termYear + ".");
//stamp.setFormFlattening(true);
stamp.close();
} catch (IOException e) {
errorFound = true;
e.printStackTrace(System.err);
} catch (DocumentException e) {
errorFound = true;
e.printStackTrace(System.err);
}
} else {
final String officialFile = "OfficialVerification.pdf";
try {
response.setHeader(contentDisposition, "attachment; filename=" + officialFile);
reader = new PdfReader(this.getServletContext().getResource("/pdf/" + officialFile));
stamp = new PdfStamper(reader, response.getOutputStream());
form = stamp.getAcroFields();
form.setField("date", current);
form.setField("reply_line", replyLine);
form.setField("first_middle_last", fmlName);
form.setField("status", studentStatus);
form.setField("hr", hoursTaken);
form.setField("term_year", termYear);
form.setField("start_end_date", termStart + " - " + termEnd);
//stamp.setFormFlattening(true);
stamp.close();
} catch (IOException e) {
errorFound = true;
e.printStackTrace(System.err);
} catch (DocumentException e) {
errorFound = true;
e.printStackTrace(System.err);
}
}
} catch (NamingException e) {
e.printStackTrace(System.err);
} catch (SQLException e){
e.printStackTrace(System.err);
} finally {if (stmt != null) try {stmt.close();
} catch (SQLException e){
e.printStackTrace(System.err);
} if (rs != null) try {rs.close();
} catch (SQLException e){
e.printStackTrace(System.err);}
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace(System.err);
}
}
Upvotes: 0
Views: 1051
Reputation: 1413
when u make the stamp.close()
call, the output stream is also flushed. since you set the response headers before stamp.close()
, the response headers are also written to the client. consequently, this causes the file download dialog to appear on the client. when the client clicks 'cancel', the http connection is terminated.
your servlets need to maintain the http connection throughout its execution as it will be writing output to the response output stream. if the http connection is terminated before the response has been committed, you will get the exception that you are seeing now.
Upvotes: 1