File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Critical ::Download data in .CSV file problem with navigation,html writes  to CSV Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Critical ::Download data in .CSV file problem with navigation,html writes  to CSV" Watch "Critical ::Download data in .CSV file problem with navigation,html writes  to CSV" New topic
Author

Critical ::Download data in .CSV file problem with navigation,html writes to CSV

Narendra Payidiparthi
Greenhorn

Joined: Sep 19, 2007
Posts: 10
Hi,
I am new to JSF and am struggling with navigation.

I tried using CommandLink and CommandButton to invoke the action in the backing bean.

I am either getting html code written in the .CSV file or the click on the
link is working in alternate attempts.. Please respond as soon as possible


Below is the code in the backing bean :

public String getHardCopyContactsFilterResultsAsCSV(){
this.setFilterReportPrefId("HARDCOPY");
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
response.setHeader("Content-disposition",
"attachment; filename=Hard Copy Contacts Fliter Results Report.csv");
E contecresponse.setContentLength(getHeaderForFliterResultsReport().length());
response.setContentType("application/text");
//response.getWriter();
try {
response.getOutputStream().write(getHeaderForFliterResultsReport().getBytes());
if(this.hardcopyResultsDTO!=null){
response.getOutputStream().write(getHardCopyContactsForFilterReport().getBytes());
}
response.getOutputStream().flush();
response.getOutputStream().close();
//context.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return "temporaryPage";
}
Note:: I Tried to comment and uncomment the context.reponseComplete() .

Case AWhen The above line is commented i.e responseComplete is called what I am observing is once I click the link to view the Results as .CSV First time I am able to save the results once I try to clik the se cond time the page is getting refreshed rather than opening the dialog as shown for the first attempt.
Every alternate attempt it is working as required i.e opening a dialog.

Case B:
Here the line responseComplete is commented. So when click on link every time a dialog opens
But what I see in te saved .CSV file is the Html code also rendered after the results written in the JSP
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
And if you consider creating a Servlet to generate and download the file?
Narendra Payidiparthi
Greenhorn

Joined: Sep 19, 2007
Posts: 10
So, you mean we cannot handle in the case of JSF and need to make a servlet call.
Narendra Payidiparthi
Greenhorn

Joined: Sep 19, 2007
Posts: 10
Also let me know how we can close the background HTML page ( The Blank Page)
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
Once I faced that problem. And I finally decided to do it by a Servlet and everything worked fine. I think when the reponse is completed by the JSF cycle something happen and thats why it is not working. So I think the servlet is an excellent option for your problem.
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
I don't understand this part. Please explain it so I can try to help you.

Originally posted by Narendra Payidiparthi:
Also let me know how we can close the background HTML page ( The Blank Page)
Joe Matthew
Ranch Hand

Joined: Jun 10, 2008
Posts: 66
I don't know of a way to close a window in JSF. Javascript will be best suited for this purpose.


Regards,<br />Joe<br /> <br />"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."<br />--Martin Golding
Narendra Payidiparthi
Greenhorn

Joined: Sep 19, 2007
Posts: 10
My question is When I try to achieve this using servlet , the dialog for Open/Save opens in the new window corresponding to the location of the
Servlet URI path.

I need to close the background HTML once I open/save/Cancel the dailog to load the CSV . I hope you got it
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
Ok. From JSF I think it is impossible to close the Window in which the request for the csv file was created. I think this could be done with javascript.

This problem you can find it solved here. Try it and if you have any question, I will be glad to help you.


Originally posted by Narendra Payidiparthi:
My question is When I try to achieve this using servlet , the dialog for Open/Save opens in the new window corresponding to the location of the
Servlet URI path.

I need to close the background HTML once I open/save/Cancel the dailog to load the CSV . I hope you got it
Narendra Payidiparthi
Greenhorn

Joined: Sep 19, 2007
Posts: 10
Andres,

You are giving me useful info .. Thanks.

I am using the below output link to make a servlet request

<hutputLink style="c14_green_under" target="_new" value="/pz3/CzDownloadCSV?&prefId=FAX&annmtId=#{announcementBackingBean.annmtId}" rendered="#{!empty announcementBackingBean.faxResultsDTO}" >
<hutputText styleClass="c14_green_under" value="View Fax Contacts Results as .csv"/>
</hutputLink>

Once I clink on the link the request is posted to Servlet when I am
setting the response to

response.setHeader("Content-disposition",
"attachment; filename=Hard Copy Contacts Fliter Results Report.csv");
response.setContentLength(getHeaderForFliterResultsReport().length());
response.setContentType("application/text");

So this code tries to open a popup with a balnk window.

As I am not using javascript to open the PopWindow I do dnot have handle to close the popup. And moreover as the request goes to Servlet , I wish to close from Servlet if any possible.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Critical ::Download data in .CSV file problem with navigation,html writes to CSV
 
Similar Threads
jsf redirect error
Content in the exported csv filefrom the java file contains the unwanted character
Return PDF via Response Stream.
while opening csv file, dialog box opens twice
Doubt on target=_BLANK