Generate MS-Word from Servlet will not work in IE ??
Today I spent a very long time to figure out why my JEE Servlet was unable to generate a dynamic MS Word document. - No the servlet dose generate a dynamic MS Word document!. But IE 7.0 in Windows XP with MS Office 2007 was not able to open the generated MS Word document! This was strange because using Firefox on Windows or also using Firefox on Linux Plattform works perfect! So why the hell IE on windows did not open MS Word?
After a long long frustrating period I found out that the solution is to not only set the ContentType to 'application/ms-word' but also set two additional header attributes to get word open on Windows Plattform.
So my servlet output code looks like this - at it works:
public class TestServlet extends javax.servlet.http.HttpServlet {
HttpServletRequest request;
HttpServletResponse response;
protected void doGet(HttpServletRequest arequest,
HttpServletResponse aresponse) throws ServletException, IOException {
this.request = arequest;
this.response = aresponse;
String sEncoding = "utf-8";
response.setCharacterEncoding(sEncoding);
response.setContentLength(5);
response.setContentType("application/msword;charset=utf-8");
response.setHeader("Content-disposition", "attachment; filename"
+ "Example.doc");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "cache");
response.setHeader("Cache-Control", "must-revalidate");
ServletOutputStream out = response.getOutputStream();
out.println("Hallo");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
A helpful thread about this topic can be read here:
http://forums.sun.com/thread.jspa?threadID=586671
I can not understand wy Microsoft always needs so much welfare? "Convention over configuration" seems not be the rule for Microsoft.....
Posted at 10:42PM Nov 07, 2008
Posted by: Ralph
Category: General