wood burning stoves
The moose likes Servlets and the fly likes RequestDispatcher and MIME types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher and MIME types" Watch "RequestDispatcher and MIME types" New topic

RequestDispatcher and MIME types

Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
I have run in to a problem with RequestDispatcher not always setting the MIME type.

In the application I am supporting, a user has a list of links that point to a servlet front-end controller. The controller uses the request parameters as well as some session info to determine which page is appropriate, and then uses RequestDispatcher to forward the request to that page.

When the user selects a link, a new window is opened (with javascript) and in theory the page in the new window will contain the requested page. When I run it in Tomcat, it works perfectly. But our production server is iPlanet/Sun Web Server, and it returns the page without any content type set. Mozilla browsers will only display the pages as text instead of processing the html.

If I explicitly set the content type on the response object before forwarding the request, it works fine... but I know this is not a good solution, as you should never commit anything to the response before forwarding it. Has anyone else run into this type of problem?

One other thing, the problem with the content type only happens when the requested resource is an html page. If it is a jsp page, it works fine, and if it is a pdf file it works fine. I could convert every html page into a jsp, but that would involve changing the file extension on 300+ MB of html. Plus, new content is being added frequently by our web development team, and I don�t want to have to police them constantly to make sure an html page doesn�t slip in there. Thanks in advance.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
Well, you could set the content-type in the HTML itself with a meta tag. This would still mean you have to change all HTML files and police everyone so that no page goes without the meta tag:

But maybe you don't have to change the HTML files at all. Just try to add this to your web.xml:

Good luck
[ June 15, 2005: Message edited by: Henrique Sousa ]

Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Thanks for the quick reply Henrique,

I looked at some of the html pages that are being requested, and they actually do contain the following tag:You would think that this would alleviate the problem, but it doesn't... I seem to remember something about mime-mapping as well. Thanks for reminding me, and I will also look to see if that is a possibility.
I agree. Here's the link: http://aspose.com/file-tools
subject: RequestDispatcher and MIME types
It's not a secret anymore!