how to display response in new window
wingman Aug 11, 2012 1:53 PMHi how to display data in new window though servlet
here am calling servlet from my managed bean
<h:form> | ||||||
<h:commandLink id="viewFileData" action="#{viewFileDataMBean.downloadFile}" value="fileName"></h:commandLink> |
</h:form>
and my managed bean has the method like
public void downloadFile() throws IOException, SQLException{
String url = "/DisplayFile ";
FacesContext context = FacesContext.getCurrentInstance();
try {
context.getExternalContext().dispatch(url);
}catch (Exception e) {
e.printStackTrace();
}
finally{
context.responseComplete();
}
and my Servlet code is
public class DisplayFile extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
File f = new File("C:/files/123.txt");
int length = 0;
ServletOutputStream op = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType( "E:/images/123.txt" );
System.out.println("......"+mimetype);
response.setContentType("text/html");
response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
response.setContentLength( (int)f.length() );
response.setHeader( "Content-Disposition", "inline; filename=\"" + f.getName() + "\"" );
byte[] bbuf = new byte[2024];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();