• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Httpservletresponse and servletoutputstream in Portlet JSF

 
Sonara Rahul
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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.
 
Dhiren Joshi
Ranch Hand
Posts: 463
  • Mark post as helpful
  • send pies
  • 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
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!