• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Getting Httpservletresponse and servletoutputstream in Portlet JSF

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

The below code is working fine in a DynamicProject created in RAD.But in portlet project its not working.I'm getting null pointer exception for httpresponse,so I made it static.BUt still even though i get httpservletresponse instance I'm getting null pointer exception in getting servletoutputstream.
I had used externalcontext but still I get null pointer exception while trying to get ServletoutputStream instance.

My requirement is when I click on the command button or commandLink I'm calling a method which converts byte array to filetype specified.But I get null pointer exception when I try to get servletoutputstream.If someone has faced the same pbm and got the solution please let me know.
I'm posting my code here.

public void doClickAction(ActionEvent event)
{
try {
File f=new File("c:\\test.html");
HttpServletResponse httpServletResponse= FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
httpServletResponse.setHeader("Content-disposition", "attachment; filename=" +"test.html");
httpServletResponse.setContentLength((int) f.length());
httpServletResponse.setContentType("application/x-download");
servletOutputStream.write(getBytesFromFile(f));
} catch (IOException e) {
e.printStackTrace();
}
FacesContext.getCurrentInstance().responseComplete();
}

Thanks,
Sonara.
 
Ranch Hand
Posts: 463
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either way what you are trying can not be achieved from portlets.
Youa re trying to open a binary dowloadable file through portlet.
Portlets do not recognize any MIME type except text/html.
So also the httpservletresponse needs to be casted to a websphere specfic servlet response .
Anyway the correct way of handling such calls and I need to send a PDf output is that you invoke a link from your portlet page which in turn invokes a servlet in your web app and do the necessary content header setting in the servlet and write directly the stream output form servlet without going to the portlet.
HTH
Dhiren
 
Where does a nanny get ground to air missles? Protect this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic