public class PdfRH { public void doRequest(ControlBlock oSCB) throws Exception {
HttpServletResponse response;
response = oSCB.getHttpServletResponse();
String pdfPath = oSCB.getRequestParameter("printpdf") ;
System.out.println("*********************************************"+pdfPath);
File pdf = new File(pdfPath);
String pdfName = pdfPath.substring(pdfPath.lastIndexOf("/") + 1, pdfPath.length());
ServletOutputStream stream = null;
BufferedInputStream buf = null;
String code_windowClose = "<script>window.onunload = refreshParent;function refreshParent() {alert('yeah!!!!');window.opener.location.reload();}</script>";
try{
stream = response.getOutputStream();
stream.write(code_windowClose.getBytes());
// PrintWriter pw = response.getWriter();
response.setHeader("Content-type","application/pdf");
response.setHeader("Content-disposition","inline; filename=" + pdfName);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setContentType("application/pdf");
FileInputStream input = new FileInputStream(pdf);
// response.setContentLength((int) pdf.length());
buf = new BufferedInputStream(input);
int readBytes = 0;
/*pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");*/
//response.getOutputStream().write(b);
while ((readBytes = buf.read()) != -1)
stream.write(readBytes);
}
catch(Exception ioe){
throw new ServletException(ioe.getMessage());
}
finally
{
if (stream != null)
stream.close();
if (buf != null)
buf.close();
}
}
PDF is not shown . the above java code is called on call of a JavaScript function to show a PDF on the webpage and on closing the window in which PDF is shown the parent jsp should reload so i have taken one variable that is String code_windowClose and i am writing the script function, converting the above variable to byte array and passing byte array to stream.write(b) the unload functionality is working fine but the problem is PDF is not shown and it is showing code like "%PDF-1.5 %µµµµ 1 0 obj <>>> endobj 2 0 obj <> endobj 3 0 obj <>/Pattern<>/Font<>/XObject<>/ProcSet[/PDF/Text/Ibs/S/StructParents 0>> endobj 4 0 obj <> stream xœT]kÛ@|è?ìã]¨Ï»÷-ÆIC71ô!ôA8Ži‰ej»”öõ_vO’[9²Ý>aä;öfvgFà œŸÇ£Û+À‹ ¸¼Á—œA•gÛ‹Mž,~#zB»Wý|–gïó ®Ç#€õ˜´µd1MÊív¾ndobj 8 0 obj <> endobj 9 0 obj <> endobj 10 0 obj <> endobj 11 0ñ¹ c"Þ\5ÆhYcPPVAPÑ÷¸F£ˆˆš˜šèU÷“•¢»§ša°~§‡3ýUuuuý" , previously before adding the unload functionality PDF was shown properly