tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: "mime-mapping" and Content-Type
Date Mon, 06 Mar 2017 17:22:09 GMT
Hash: SHA256


On 2/27/17 9:50 AM, Ryan Yohnk wrote:
> I wasn’t aware there was a servlet in-between simple requests like 
> you stated. But that makes perfect sense.

Something has to serve the bytes. In a Java web application server,
that's called the DefaultServlet.

> As noted in the SO post, I can see that 
> StandardContext:addMimeMapping() is being called with my specific 
> mapping during initialization. But I’m never seeing the backing 
> mimeMappings called from there.

Right: because you are never consulting that mapping.

> I’ve seen quite a few online posts on this topic but can’t seem 
> find any official documentation for 8+. Is this feature something 
> that’s still supported? If so, is there something I’m overlooking 
> in my test or configuration?

I think you are misunderstanding what's going on entirely.

> Any light you guys can shine on the subject would be greatly 
> appreciated.

If you have a file on the disk called foo.swa, and you request it
using and the DefaultServlet serves
the bytes off the disk, then the DefaultServlet will use the .swa
extension plus your mime-mapping to produce the "Content-Type:
text/rtf;charset=UTF-8" response header.

In your situation, your servlet is handling the request, and the
DefaultServlet isn't involved. Tomcat doesn't magically add response
headers based upon the URLs used to choose the servlet to serve the
bytes. Your own servlet is responsible for setting whatever response
headers it wants to use.

Tomcat will automatically add the following headers:

- - Date
- - Content-Encoding: chunked (if the response is committed and not yet
- - Content-Length (if not chunked)

... but nothing else. If you want a Content-Type header, you'll have
to add it yourself.

Here's your code:

> Servlet:
> public class SimpleServlet extends HttpServlet { protected void
> doGet(HttpServletRequest req, HttpServletResponse resp) throws
> ServletException, IOException { IOUtils.write("Hello There!",
> resp.getOutputStream()); resp.setStatus(202); } }

You aren't writing any Content-Type header, so you aren't getting one.
If you want to add a Content-Type header, just do it:

public class SimpleServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req,
                       HttpServletResponse resp)
    throws ServletException, IOException {
    IOUtils.write("Hello There!", resp.getOutputStream());

Or, if you want to detect the correct MIME type from the URL, you
could do this:

  ServletContext app = req.getServletContext();
  String mimeType = app.getMimeType(req.getRequestURI());

>> You said you don't want to modify the source... what about
>> *adding* to it? Would it be okay to write a Filter and add it to
>> the configuration of the application? That is a very easy thing
>> to do, and you can add whatever headers you want for whatever
>> reason.
> Yep, a filter will be the next step. Honestly that’s the behavior 
> I’m describing anyway. My initial hope was to keep the changes 
> limited to Tomcat configuration but if that’s not possible that’s 
> cool.
> Since it seems like a pretty common filter, is anyone aware of an 
> existing implementation? I know it’s pretty trivial but no sense
> in rewriting the wheel if it’s already out there.

I'm not sure it's that common of a request. Usually the servlet knows
the type of data it returns, and sets the content-type accordingly. I
haven't seen many servlets that sometimes serve PDFs and sometimes
server plain-text responses, and can only tell the difference when the
request URL has a particular filename-looking-thing in it that has a
recognizable "file extension" (which is just a convention, of course).

If you wanted to write a filter that could do this kind of thing, it
would be only a few lines long:

public class MimeTypeFromURIFilter
    implements Filter {
  // ignoring init() and destroy() methods

  public void invoke(ServletRequest request,
                     ServletResponse response,
                     FilterChain chain)
    throws ServletException {

    // Don't clobber any existing content-type
    if(null == response.getContentType()) {
      ServletContext app = req.getServletContext();
      String mimeType = app.getMimeType(req.getRequestURI());

    // You could also put this before the Content-Type check,
    // but it's possible that the response could be committed
    // by the filter chain (or servlet) and then the Content-Type
    // header wouldn't be included in the response.
    chain.doFilter(request, response);

Hope that helps,
- -chris
Comment: GPGTools -
Comment: Using GnuPG with Thunderbird -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message