File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Using response.setContentType to switch HTML to Word Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using response.setContentType to switch HTML to Word" Watch "Using response.setContentType to switch HTML to Word" New topic
Author

Using response.setContentType to switch HTML to Word

craig motell
Greenhorn

Joined: Oct 03, 2008
Posts: 6
I have a question which is both a best practice and priorty question.

I have a simple jsp which will show output in one of three formats:
html, word, or excel.

When I use index.jsp outputs html, when I use index.jsp?format=word outputs as a word document on Internet explorer.

My question is what takes priorty, the response.setContentType or the
<head>
<meta http-equiv="Content-type" content="text/html" ...

the answer must be the response.setContentType but the answer isn't obvious to me.

Can anyone tell me how one should set up the response.setContentType("text/html") or ("application/msword").

That is, is there a semi-standard for dealing with this issue or position in the jsp file where you should put switches over content type?

Thank you,

----------------------------- code below ------------------------------

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My Response Type Test</title>
</head>
<%
if (request.getParameter("format") != null) {
if (request.getParameter("format").equals("word")) {
response.setContentType("application/msword");
} else if (request.getParameter("format").equals("excel")) {
response.setContentType("application/vnd.ms-excel");
}
}
%>
<body>
<h2>Testing Word Document Format</h2>
<table border="1">
<tr>
<th>Team</th><th>QuarterBack</th><th># Super Bowls</th><th>Rating</th>
</tr>
<tr>
<td>49ners</td><td>Joe Montana</td><td>4</td><td># 1</td>
</tr>
<tr>
<td>Patriots</td><td>Tom Brady</td><td>3</td><td># 2</td>
</tr>
<tr>
<td>Steelers</td><td>Terry Bradshaw</td><td>4</td><td># 3</td>
</tr>
<tr>
<td>Broncos</td><td>John Elway</td><td>2</td><td># 4</td>
</tr>
</table>
</body>
</html>
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Craig,
Welcome to JavaRanch!

How are you outputting Word formatting data in a JSP? Are you using a library like POI? If so, it is advised to put such Java code in a servlet and not in the JSP.

Or are you trying to open an HTML document in Word? This is likely to cause you problems. Office 2007 gives a security warning if the file extension does not match the content. Which means HTML in a .doc file will gives this warning.

That is, is there a semi-standard for dealing with this issue or position in the jsp file where you should put switches over content type?

This is actually easier to answer because it's general. If I am understanding what you are tyring to do, you have three different output formats in the same JSP. It would be better to have different JSPs (or servlets for the Word/Excel ones) for each format.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
My question is what takes priorty, the response.setContentType or the
<head>
<meta http-equiv="Content-type" content="text/html" ...

the answer must be the response.setContentType but the answer isn't obvious to me


I was playing around with the example you quoted and see that only "response.setContentType" actually does the required thing(like opening MS-WORD for application/msword). There is no effect of setting <meta http-equiv="Content-type" content="application/msword" ...

What could be the reson for this? Are doing these two things any different from each other?
[ October 15, 2008: Message edited by: Satya Maheshwari ]

Thanks and Regards
craig motell
Greenhorn

Joined: Oct 03, 2008
Posts: 6
Thanks for the responses.

My intention is NOT to do this in a servlet because I figure I'm just outputting HTML. First, thank you, I'll check out the Apache POI. Maybe that is the approach I want and I'll test against Internet 7.0.

But what I like was the fact that I could have this HMTL file, with a table in it. Then if users wanted to "print" this to the printer, the quick and dirty way I would use is to convert it to word via the "setContentType".

Jeanne Boyarsky, also suggested the use of a servlet to print out stuff in three different formats (html, doc, and excel). I don't know what a word format would be. Would I still use html tags?

Anyway, I'll check out Apache POI and also I saw another link along similar discussion lines at http://www.herongyang.com/jsp/response_header_5.html.

aloha craig
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Craig,
The user couldn't just print it in the browser?

In Office 2003, you could get away with calling an HTML file a Word document and having it open in Word. In Office 2007, you get the security prompt so this is discouraged. POI will give you the binary Word format so it is a real word document.

If you just have HTML, putting in a JSP is fine. I think you'll have "real Java code" once you get into Excel and Word at which time you can use a servlet for the two formats.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using response.setContentType to switch HTML to Word
 
Similar Threads
how to display output in the same page of imput
Display error when record not found
How to Open Excel file from JSP page.
Exporting HTML Data to excel issue
Problem With jsp:forward