• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Using response.setContentType to switch HTML to Word

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
craig motell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic