aspose file tools*
The moose likes Servlets and the fly likes setting name of requested pdf in browser's title Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "setting name of requested pdf in browser Watch "setting name of requested pdf in browser New topic
Author

setting name of requested pdf in browser's title

Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi,
I have certain protected resources in my web application which requires user login. If login is successful, then we forward the user to the specified pdf resource.

Problem which I am facing is that after the login is successful, title of the browser remains the title of the login page. Our requirement is that it should display the title of the requested pdf instead of the login page title.

I did lots of r&d on http headers but no luck.

Is it possible to show the name of the requested pdf on the browser's title instead of the title of the login page?

My pdfs are in a content repository OpenCMS. Title is already set for all the pdfs.

Thanks

Naseem
[ February 22, 2007: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Originally posted by Naseem Khan:
I did lots of r&d on http headers but no luck.
Thre is no HTTP header that will affect the browser title.

The only way to set the title is withthe <title> tag in the <head> of an HTML document. So if you're not serving an HTML page, you don't have a means to set the title.

When you serve the PDF, are you navigating away from the login page?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Originally posted by Bear Bibeault:

When you serve the PDF, are you navigating away from the login page?


Yes if login is successful, user is forwarded to a reporting servlet which does two thing in single transaction, first it logs the user details in the database and then it forwards to the pdf. Beside forward, I tried reading pdf byte by byte as well.

Redirect puts the whole flow in infinite loop as the same browser send second request which is same as first.

Naseem
[ February 23, 2007: Message edited by: Naseem Khan ]
Subhadip Chatterjee
Ranch Hand

Joined: Dec 12, 2006
Posts: 93
Hi Naseem,
well, what i think is, if you have got the Servlet to do the login validation, and if your reading the PDF content & setting into response (of course after changing the response content-type), then only way you can do that, is you have to pre-fetch the name of PDF, after validation, and then set into request attribute. Next fetch the PDF content, and while writing the response into the forwarded page (next page after loin-validation), take the PDF name from request, and use server-side scripting to set the title in <title></tile> tag under <head> of your html/jsp page.
Let me know, if this works!!!


Refreshing life every moment...
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Well, firstly pdf is not in the file system rather its coming from the database by content management system. So we can't read the pdf by java's FileInputStream class.

I tried your second suggestion i.e., fetching the name of the pdf and writing the <title>fetched_pdf_name</title> tag in the response but it will work only when content type is text/html but in our case content type is not text/html, rather its application/pdf.

Thanks

Naseem
[ February 23, 2007: Message edited by: Naseem Khan ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Rather than having your servlet forward to the PDF, forward to a JSP or HTML page that sets the title and initiates a redirect to the PDF. That will give you the opportunity to set the title.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Originally posted by Bear Bibeault:
Rather than having your servlet forward to the PDF, forward to a JSP or HTML page that sets the title and initiates a redirect to the PDF. That will give you the opportunity to set the title.


Hi Bear,
Thanks for your reply. I tried with redirect but it puts the whole flow in infinte loop.

Forwarding to pdf from jsp is working only once. Second time whenI access the pdf, server throws exception...

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Originally posted by Naseem Khan:
Forwarding to pdf from jsp is working only once. Second time whenI access the pdf, server throws exception...
That's right. You cannot forward after writing output.
I tried with redirect but it puts the whole flow in infinte loop.
Find out why and fix it. Your redirect shouldn't address the start of the process, just the PDF streaming portion.
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
To get the browser to list the correct "title" of the pdf, the best solution I've found is to send your redirect to MyReportNameForTitle.pdf and in web.xml map *.pdf requests to your pdf servlet. If your redirect was to "MyReportNameForTitle.pdf," then that's typically the file name that the borwser displays.


Fun programming etcetera!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Originally posted by Dave Wingate:
To get the browser to list the correct "title" of the pdf, the best solution I've found is to send your redirect to MyReportNameForTitle.pdf and in web.xml map *.pdf requests to your pdf servlet. If your redirect was to "MyReportNameForTitle.pdf," then that's typically the file name that the borwser displays.


Are we talking about different things here?

By "title" does the original poster mean the true title attribute of the window which displays as the window caption as I was assuming, or the URL as Dave Wingate is suggesting?

Which is it?
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
I am talking about showing title of the pdf at the browser's title bar, currently its showing me http://hostName/login.do.

Forwarding to pdf on IE Version 7 from the login page makes title of the browser htp://hostName/login.do and browser's tab shows the title of the login page.

Secondly I already configured pdfs url pattern by an error code. (Resource forbidden)



As Bear suggested, forwarding to a jsp page and setting the title there by title tag and redirecting to pdf is the way to go. But the only problem which I am facing is everytime authenticate.do gets called which puts the system in infinite loop.

If somehow I can bypass the call to authenticate.do second time from jsp, then and only pdf will be displayed to end user along with title.

Flow is something like this:

If user is not logged in:

authenticate.do -> login.jsp -> login.do -> reporting.jsp


This reporting.jsp logs the user information in database and redirects to pdf.

If user is logged in:

authenticate.do -> reporting.jsp


Regards
[ February 26, 2007: Message edited by: Naseem Khan ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Originally posted by Naseem Khan:
But the only problem which I am facing is everytime authenticate.do gets called which puts the system in infinite loop.


Don't redirect back to the authentication action. You've already authenticated, no need to do it again. Redirect to the servlet that will serve up the PDF. Be sure to set the content headers so that the PDF is served as an attachment (search for content-disposition for more info).

Your authentication action can place information in the session that the PDF servlet can use to make sure that only authenticated requests are satisified.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
I tried reading pdf by opening a connection to the url but it does nothing. Same login page opens again.

I want to open the pdf in the browser itself instead of as an attachment.


[ February 27, 2007: Message edited by: Naseem Khan ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Originally posted by Naseem Khan:

I want to open the pdf in the browser itself instead of as an attachment.


Then you will not be able to set the page title using HTML. You can serve either an HTML file, or a PDF, not both.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting name of requested pdf in browser's title