0
votes

Here's my JSP code. It was working fine some days back. But I'm unable to view the pdf file now.

<% String email = (String) session.getAttribute("email"); %>
<%
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
String sql ="select resume from signup where email = ?";
ps = connection.prepareStatement(sql);
ps.setString(1,email);
resultSet = ps.executeQuery();
while(resultSet.next()){
    byte [] resumeBytes = resultSet.getBytes("resume");

%>
<!DOCTYPE html>
<html>
<head>
<title>Your Resume</title>
</head>
<body>
<%
    DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
    response.setContentType("application/pdf");
    for(int i = 0; i < resumeBytes.length; i++){
    dataOutput.write(resumeBytes[i]);   
    }
}
%>

"getOutputStream() has already been called for this response" is what the Exception is. Please help with this

1

1 Answers

0
votes

i will try to explain what i know

**Basic rule of HTTP: one request, one response.

You can only send back one thing to a request. Either an HTML page, or a PDF document, or an image or.... Specifically you can't send an HTML page AND a PDF document. Java complains if you have already obtained a writer/outputstream as you should only be getting ONE of these.**

I guest won't work since you've already sent a PDF back to the browser. Your execute() method should be returning 'null' to tell Struts not to do this.

And it could be better to do it in servlet, forward it to new jsp page, and also add try and catch blocks, and finally block too, or simply you can use decorative pattern with your try and catch, so when Stream ends it automatically will close any open resources.

I wish I've been helpful.