• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

displaying pdf file

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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("text/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();
}
}
}
Displays an error page.Init is called.Kindly help.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change content type header to application/pdf
ie use
response.setContentType("application/pdf");
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same error page displayed when using (application/pdf)
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me if this is a stupid question but are you invoking the servlet with a GET or POST request?
    Bookmark Topic Watch Topic
  • New Topic