Trying to open the pdf file mentioned in the code.The init method is called but an error page is dispayed.Where am i going wrong.Plz help.I'm using jsdk2.0 to
test.
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletPdf extends HttpServlet
{
public void doPost (HttpServletRequestrequest,
HttpServletResponse response)
throws ServletException, IOException
{
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
ServletOutputStream out =
response.getOutputStream ();
String title = "Simple
Servlet Output";
response.setContentType("application/pdf");
String fileURL ="C:/servletxml/melly/ch03.pdf";
response.setHeader("Content-disposition","attachment; filename=" +"Example.pdf" );
try {
URL url = new URL( fileURL );
// Use Buffered Stream for reading/writing.
bis = new BufferedInputStream(url.openStream());
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch( MalformedURLException e) {
System.out.println ( "MalformedURLException." );
throw e;
} catch( IOException e) {
System.out.println ( "IOException." );
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}