JSF command button can be clicked only once when calling servlet for file download

200 views Asked by At

I have some command buttons in a jsf, one of which when clicked when create a file and download. In the action class that handle the jsf action , I create the url object that has the URL for calling the Servlet. This all works , the file is downloaded one time when I click on the button , but the issue is , I cannot click on the button or any other command buttons on the page after this. Why is the request not complete? Please help.

<h:commandButton id="filedownloadbtn" action="#{fileDownloadInit.submit}" value = "thisform">

Action

 try {

String             baseURL =  facesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
          String             url            = baseURL + "/DataloadServlet";


            facesContext.getCurrentInstance().getExternalContext().redirect(url);
            return null;
        } finally {

            facesContext.responseComplete();
        }'

DataloadServlet

 public Object[] getFileNameAndData(HttpServletRequest request)
    {   

            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            //does some processing...

            return new Object[] {fileName, stream.toByteArray()};
}

FileDownloadservlet

public abstract class  FileDownloadservlet extends javax.servlet.http.HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        Object[] file = getFileNameAndData(request);
            if (file != null)
            {
                String fileName = (String)file[0];
                byte[] fileData = (byte[])file[1];

                response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName +"\"");



                    response.setHeader("Cache-Control", "no-cache, no-store"); // HTTP 1.1.
                    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.


                String contentType = "application/vnd.ms-excel";
                response.setContentType(");
                response.setContentLength(fileData.length);
                try
                {
                    OutputStream output = response.getOutputStream();
                    output.write(fileData);
                    output.flush();
                    output.close();
                }
                catch (IOException ex)
                {

                }
            }

}
0

There are 0 answers