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((monone yURL) , 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/p df fullscreen=yes"%>
and in the struts.config file I have declared the following :
<global-forwards>
<forward name="monroney" path="/WEB-INF/jsp/dlrTnRc v/monroney .jsp" />
</global-forwards>
<action-mappings>
<action path="/monroney"
type="controller.dlrTnRcv. MonroneyAc tion"
name="commonForm"
scope="request"
input="/WEB-INF/jsp/dlrTnR cv/monrone y.jsp"
validate="false">
</action>
</action-mappings>
In the MonroneyAction class I am calling the method of the servlet.
CreatePDF.processMethod(re quest, 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
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((monone
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/p
and in the struts.config file I have declared the following :
<global-forwards>
<forward name="monroney" path="/WEB-INF/jsp/dlrTnRc
</global-forwards>
<action-mappings>
<action path="/monroney"
type="controller.dlrTnRcv.
name="commonForm"
scope="request"
input="/WEB-INF/jsp/dlrTnR
validate="false">
</action>
</action-mappings>
In the MonroneyAction class I am calling the method of the servlet.
CreatePDF.processMethod(re
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
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.methodW hichMakesP DF(valueOb ject);
request.getSession().setAt tribute(se ssionKeyNa me,pdfByte Stream); return mapping.findForward("PDFSu ccess");
the above code call the method ( methodWhichMakesPDF(valueO bject);) 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.AboveAct ionClass" 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.setCon tentType(" applicatio n/pdf");
so what will happen is all the pdf data from session will open up in a pdf file.
hope that helps
J
String sessionKeyName = "PDF_BYTESTREAM";
ByteArrayOutputStream pdfByteStream = null;
pdfByteStream = ClassWhichMakesPDF.methodW
request.getSession().setAt
the above code call the method ( methodWhichMakesPDF(valueO
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.AboveAct
<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.setCon
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks jaggernut! This solution was awesome.
I am happy it worked.
thanks for the points
J
thanks for the points
J
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