aspose file tools*
The moose likes JSP and the fly likes Showing PDF in a new window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Showing PDF in a new window" Watch "Showing PDF in a new window" New topic
Author

Showing PDF in a new window

Neo Parker
Greenhorn

Joined: Aug 03, 2005
Posts: 7
I am using Websphere to deploy my app. My application is Struts-JSP.

Now, I have a PDF in the form of byte stream in my Action. I want to pass this byte stream to the JSP, and have a new window popped up showing the PDF. I don't want to store it as a physical file in the server context or at the client side. Is it possible? If not, what is the best way to achieve this (showing the PDF).

Thank you very much in advance!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I generally do this sort of thing 'the other way around'.
I build a JSP page that has a link to the servlet that streams the PDF.

Remember, you can't send both binary data (PDF) and text (HTML) in the same response.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Neo Parker
Greenhorn

Joined: Aug 03, 2005
Posts: 7
But I can store the byte stream as a Form attribute and access it in the JSP right? If it is a Servlet, I will just send the byte stream in the response.

Since this is struts and jsp, I want to access the byte stream in the jsp using the attribute, and then show a new window with the PDF.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Neo Parker:
But I can store the byte stream as a Form attribute and access it in the JSP right?


I'm not sure what you mean by this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

No, you can't.

What you should do is to place Javascript in the JSP which will cause a redirect to occur to a servlet that streams the PDF. By setting the content-type and content-disposition correctly (search this forum for these terms) the response should cause the download box to pop up and then the PDF can be shown in its own viewer.

Having the bytes of the PDF available in the JSP is useless to you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Neo Parker
Greenhorn

Joined: Aug 03, 2005
Posts: 7
Thanks for your responses. Your idea would definitely work.

But the thing is, we have ActionServlet to handle everything. That's the whole point of Struts, having just one controller. So for just one functionality (showing PDF), I am really hesitant to add one separate servlet.

Isn't there a way to show PDF using just struts, jsp and javascript (without no extra servlet)?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

What part of Struts prevents you from doing any of this?
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
create an Action servlet that would generate the PDF then return null.


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

M Jiva,
Your post was moved to a new topic.
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

Hi Neo,
In struts ActionServlet is the default Controller. you are right till here, but you can create your our controller's extending one of Action,DispatchAction etc. please search for it, and create a servlet or CustomAction to generate the pdf. which you can call using javascript from your jsp.
Hope this helps


[Vipassana] - It is seeing the reality as it is, And not as you want it to be.!!!
SCJP1.5.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Showing PDF in a new window