views:

844

answers:

3

Hi

my Java based webapp has a servlet which streams PDF content back to the browser based on request parameter.

e.g. user clicks on an A tag with an href of "myApp/FetchPDFServlet?id=123". Servlet mapping picks up request, streams PDF data to response as mime-type application/pdf, closes flushes buffers.

However the browser title bar for the page displaying the PDF reads "FetchPDFServlet?id=123"

How can I change the title the browser displays for the page that displays the PDF? So the browser title is "Here is the amazing PDF" not "FetchPDFServlet?id=123".

Is it possible at all? How best to do this.

thanks for any help Adrian

A: 

You could display the PDF in an iframe.
Something like this:

<html>
  <head>
      <title>Here is the amazing PDF</title>
      <style type="text/css">
       html, body, div, iframe { margin:0; padding:0; height:100%; }
       iframe { display:block; width:100%; border:none; }
      </style>
  </head>
  <body>
    <iframe width="100%" length="100%" src="myApp/FetchPDFServlet?id=123"/>
  </body>
</html>

So instead of linking to a pdf document using myApp/FetchPDFServlet?id=123, you would link to something that returns the above html. For example, a jsp page: myApp/ShowPDF.jsp?id=123&title=Here%20is%20the%20amazing%20PDF

Francois Gravel
+2  A: 

Add this header to your HttpServletResponse:

response.setHeader("Content-Disposition","inline; filename=Here is the Amazing PDF");

I believe the browser will pick it up and use it as the title of the window.

Gandalf
A: 

I tried using first solution be still doesn't pick up the title. Please help:

 BufferedOutputStream bos = null;
 try {
  ServletOutputStream out = response.getOutputStream();
  response.setContentType(mineType+";charset=UTF-8");

  response.setHeader("Pragma", "public");
  response.setDateHeader("Expires", 0);
  response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
  response.setHeader("Cache-Control","public");
  response.setHeader("Content-Description","File Transfer");
  response.setHeader("Content-Disposition","inline; filename=My title");

  bos = new BufferedOutputStream(out);
  bos.write(xfdl);
 }
 catch (Exception e) {
  throw e;
 }
 finally {
  if (bos != null) {
   bos.flush();
   bos.close();
  }
 }
Richard Huang
Include filename as pathinfo in URL or use a better webbrowser ;)
BalusC
If you want to ask a concrete question here at StackOverflow, better post a real question by pressing the `Ask Question` button at the right top. Do not post your question as an `Answer`. Feel free to include links to topics which you found but didn't help much (and elaborate why not).
BalusC