Link to home
Start Free TrialLog in
Avatar of paromitabanerjee
paromitabanerjee

asked on

Struts - Trying to open a new window and display a dynamically generated pdf file in that window

Scenario: The user selects a Reference no and then clicks button in a jsp(this is not a form submit button). After that, a new window should open up and a dynamically generated PDF should be displayed there.

Problem: -- When I run this as a java application, the PDF file is generated fine. But I am new on Struts. and am not sure of the flow for calling and displaying this servlet.

What I have tried to do: -- In the jsp after the user clicks on the button, I pass the following URL while opening another window using javascript.
eg. var mononeyURL = "http://localhost:9080/pcnavms/monroney.do";
openResourceWindow((mononeyURL) , 600, 800, true, true, true, false);

This opens up the new window fine. And in the monroney jsp, I am simply declaring the content type
eg. monroney.jsp
<%@ page language="java" contentType="application/pdf fullscreen=yes"%>

and in the struts.config file I have declared the following :
<global-forwards>
<forward name="monroney" path="/WEB-INF/jsp/dlrTnRcv/monroney.jsp" />
</global-forwards>

<action-mappings>
<action path="/monroney"
type="controller.dlrTnRcv.MonroneyAction"
name="commonForm"
scope="request"
input="/WEB-INF/jsp/dlrTnRcv/monroney.jsp"
validate="false">
</action>
</action-mappings>

In the MonroneyAction class I am calling the method of the servlet.
CreatePDF.processMethod(request, response);

But this doesn't work. Can anyone please help me with this.

What is happening is, it opens up a new window. The File Download Box is displayed.(Save monroney.do) If I click on Save, it saves a file named monroney.do which has got nothing in it. If I click cancel, then nothing happens. And the new window, disappears anyway.

What I am trying here, is generating a PDF file(whose data is dynamically populated) which should be displayed in the new window that has opened (not save it to hard disk).

I can't understand why monroney.do is created as a file  and asks for saving it.

thanks,
Paromita
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

PS: It will help the browser greatly if you
1. name the jsp with something.pdf instead of something.do
2. make sure your CreatePDF returns the headers
content-type: application/pdf
if you want the browser to show it or
content-type: application/octet-stream
if you want the save-as dialog to show
Avatar of paromitabanerjee
paromitabanerjee

ASKER

Ok. pls move this to the JSP forum. Isn't there a forum dedicated to Struts ?
>>>this is what you should do  in your action class

String sessionKeyName = "PDF_BYTESTREAM";
ByteArrayOutputStream pdfByteStream = null;
pdfByteStream =      ClassWhichMakesPDF.methodWhichMakesPDF(valueObject);
            request.getSession().setAttribute(sessionKeyName,pdfByteStream);                        return mapping.findForward("PDFSuccess");

the above code call the method ( methodWhichMakesPDF(valueObject);)  which generates the pdf and assigns it
to pdfByteStream . and then we set pdfByteStream  to sessionKeyName and forward to "PDFSuccess"

>>>this is what you should do  in your struts-config

<action path="/forms/pdfShow" type="com.ddd.eee.AboveActionClass" parameter="method" name="formbeanname" scope="session">
            <forward name="PDFSuccess" path="/PDFServlet" /> //THIS LINE CALLS THE SERVLET
</action>

>>>>In your servlet get the pdf data which you set in SESSION (sessionKeyName) in your action class and

type
             
httpServletResponse.setContentType("application/pdf");


so what will happen is all the pdf data from session will open up in a pdf file.

hope that helps
J



ASKER CERTIFIED SOLUTION
Avatar of jaggernat
jaggernat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks jaggernut! This solution was awesome.
I am happy it worked.

thanks for the points
J