Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Message not displaying after downloading a file

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day guys,

I have a page that I want to debug, but its hard, so i need to help from u.

The codes is like this:

public void processPage() {

MessageUtil.addErrorMessage("Error Message");

downloadPDF();

return null;

}



private void downloadPDF() {
FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
response.setContentType("application/pdf");
response.setContentLength(pdf.length);

List<String> hwbs = ePODRequest.getHwbs();
String fname = utilsFactory.getPDFFilename(hwbs);

StringBuilder sb = new StringBuilder("attachment; filename=\"")
.append(fname)
.append("\"");
String removeNewLine = sb.toString().replaceAll("\\r|\\n", "");

response.setHeader("Content-disposition", removeNewLine);
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(pdf);
} catch (IOException e) {
LOGGER.error("Unable to print to outputstream", e);
}
faces.responseComplete();
}


THE question is why the Message not appear when downloadPDF() was called?
 
Saloon Keeper
Posts: 22495
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I often say, the more JSF-specific code you write, the more likely you're not doing it right. I recommend writing a standard (non-JSF) servlet or JSP when you need to return a non-HTML file to the client.

But the real limitation here is in HTTP itself, not in JSF or J(2)EE.

In HTTP, there is a strict 1-to-1 request/response architecture. You send one request, you get back one response. In this particular case, you send a request for a file, you get back an outputstream containing the file's contents. There's no room in HTTP for also sending back a confirmation message in the same request/response cycle.
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the advice..

The problem is, it is already written, i just need to fix it. So any idea on how can I send back a message?
 
Tim Holloway
Saloon Keeper
Posts: 22495
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if I remember corrrectly, the "Save As" dialog box that the client pops up won't appear until the download has completed already anyway.
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks.

Can you ellaborate on how to do that?

Really thanks
 
Tim Holloway
Saloon Keeper
Posts: 22495
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no "how to do that". It's done automatically by the client.
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean what will I do?

Use a dialog box to render message?
 
Tim Holloway
Saloon Keeper
Posts: 22495
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dialogbox is the message.

Like I said, only after the data has downloaded will the client ask where to store it. Or, alternatively, what program to run against it, depending on MIME settings.
 
You don't like waffles? Well, do you like this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic