Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6705205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:22:37+00:00 2026-05-26T07:22:37+00:00

Using JDK 1.6, JSF 2.1, PrimeFaces 2.2.1, POI 3.2, and Apache Tomcat 7 I

  • 0

Using JDK 1.6, JSF 2.1, PrimeFaces 2.2.1, POI 3.2, and Apache Tomcat 7

I am trying to setup a servlet to allow a download of an excel file based on the user selection. The excel document is created at runtime.

No errors and the code does get into the servlet.

I click the button and nothing happens. I am not using the datatable export that PrimeFaces uses because I need to do reordering and custom formatting on the data in the Excel document.

ExportExcelReports.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {       
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=\"my.xls\"");                

    HSSFWorkbook workbook = new HSSFWorkbook();

    HSSFSheet sheet = workbook.createSheet();
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellValue(0.0);

    FileOutputStream out = new FileOutputStream("my.xls");
    workbook.write(out);
    out.close();
}

ProjectReportBean.java

public void getReportData() {
    try {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = ctx.getExternalContext();
        HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
        HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ExportExcelReports");
        dispatcher.forward(request, response);
        ctx.responseComplete();
    } catch (Exception e) {}
}

index.xhtml

<h:form id="reportsForm">
    <h:outputLabel for="report" value="Reports" /><br />
    <h:selectOneMenu id="report" value="#{projectReportBean.selectedReport}" required="true" requiredMessage="Select Report">
        <f:selectItem itemLabel="---" noSelectionOption="true" />
        <f:selectItems value="#{projectReportBean.reports}" />
    </h:selectOneMenu>

    <p:commandButton action="#{projectReportBean.getReportData}" value="Export" update="revgrid" />                      
</h:form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:22:37+00:00Added an answer on May 26, 2026 at 7:22 am

    There are two problems.

    The first problem is that the <p:commandButton> sends by default an Ajax request. This request is fired by JavaScript code. However, JavaScript can’t do anything with a response which contains a file download. Due to security restrictions JavaScript can’t spawn a Save As dialogue or something. The response is basically totally ignored.

    You need to add ajax="false" to <p:commandButton> to turn ajax off so that the button fires a normal synchronous HTTP request, or you need to replace it by standard <h:commandButton>.

    <p:commandButton ajax="false" ... />
    

    or

    <h:commandButton ... />
    

    The second problem is that your servlet doesn’t write the Excel file to the response at all, but instead to a local file which is stored in server’s working directory. Basically, the HTTP response contains nothing. You need to pass HttpServletResponse#getOutputStream() to the WorkBook#write() method.

    workbook.write(response.getOutputStream());
    

    On an unrelated note, I wonder how the servlet is useful here. Do you want to reuse it outside JSF? If not, you don’t necessarily need to dispatch to the servlet at all, but just execute the same code in bean’s action method. That empty catch block is also not nice. I’d just declare it as throws in method or at least rethrow it as new FacesException(e).


    Update as per the comments you seem not be interested in the servlet at all. Here’s a minor rewrite how you could send the Excel file programmatically in a JSF action method.

    public void getReportData() throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue(0.0);
    
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.setResponseContentType("application/vnd.ms-excel");
        externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"my.xls\"");
    
        workbook.write(externalContext.getResponseOutputStream());
        facesContext.responseComplete();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JDK 1.7, Apache Tomcat 7.0.23 and I have placed JSTL core
I'm trying to code sign a JAR file and am using JDK 1.7u1. We
I am trying to use the library DigestUtils in Android 2.3.1 using JDK 1.6,
Using JDK 7 I've had success in watching specific directories for new file creations,
I am using JDK 1.6.0.18 for my Swing application. I am experiencing very different
I have a relatively simple class. I serialize it using JDK 1.7 on my
This is using the Backberry JDK (5.0 if needed). I have a custom Manager
Using IE 7, JDK 1.6 and Sun Web server 7. Inside the jsp form,
I am using JAXB 2.0 JDK 6 in order to unmarshall an XML instance
I'm using intelliJ IDEA version 9.0.1 build #IU 93.94 with JDK 1.6.0_17. This error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.