1

I referred to Coreservlets

My question is when i run following program, it starts to download(which shows nothing inside it) rather than displaying as Excel sheet on the browser as what book shows.No Exceptions happened

enter image description here

This is the program,

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // processRequest(request, response);
        response.setContentType("application/vnd.ms-excel");
        PrintWriter out= response.getWriter();
        out.println("\tQ1 \tQ2 \tQ3 \tQ4 \tQ5");
        out.println("Apples \t1 \t2 \t3 \t4 \t5");
        out.println("Oranges \t1 \t2 \t3 \t4 \t5");       
    }

I'm using Netbeans IDE with Glassfish 4.1 server which comes with Netbeans itself.Same time i' can't even build PDF's too.Am i missing plugin or something else?

AVI
  • 5,516
  • 5
  • 29
  • 38

2 Answers2

2

Do you use Internet Explorer with Windows with instaled Microsoft Office like in book? This example may not work in all browsers (Chrome, Mozila and so on).

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
2

The behavior you desire is only possible, if browser is able to open the file inside the browser window. The browser needs MS Excel installed and a plugin to open MS Excel inside the browser window (this is similar to running flash in browser - it does not work until you install the flash plugin). I don't know if such a plugin is available for chrome or firefox, as applications from Microsoft are not behaving friendly to other applications.

If all this is in place, there is a high chance you will get desired behavior. If not, you might need to add a Content-Disposition: inline into the HTTP header in the response, as described here: How to force files to open in browser instead of download (pdf)?

Like this:

response.addHeader("Content-Disposition", "inline; filename='apples_oranges.xls'");
Community
  • 1
  • 1
OndroMih
  • 7,280
  • 1
  • 26
  • 44