ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Two Servlet Filters Every Web Application Should Have
Subject:   MIME types and Servlets that generate binary data
Date:   2003-12-12 13:29:00
From:   anonymous2
Response to: MIME types and Servlets that generate binary data

Right. I know that much, but the problem is when you don't actually generate a binary file dynamically, but have the servlet essentially do the following (in pseudocode):


doGet(...){
Image img = createImageDynamically();
response.setContentType("image/jpeg");
response.getOutputStream().write(img);
}


Here, assume that servlet is named MyImageServlet. Even assume that the servlet mapping is MyImage.jpg. AFAIK, the <mime-mapping> tag won't work (since it's only for static files). What one can do is something like the following:


1) in CacheResponseWrapper, declare:
String contentType;
2) in CacheResponseWrapper, override setContentType as follows:
public void setContentType(String type){
origResponse.setContentType(type);
contentType = type;
}
3) provide an accessor for the contentType String
4) in CacheFilter, make sure to cache the results of getContentType along with the byte output stream.


I'm not sure if this is teh best way, but it works and seems not to be *too* crazy. Obviously open to better solutions.


On another note, I was wondering how we could cache pages that use cookies or sessions. Any ideas anyone?


1 to 2 of 2
  1. MIME types and Servlets that generate binary data
    2003-12-12 15:07:35  jfalkner [View]

    • MIME types and Servlets that generate binary data
      2003-12-15 09:02:11  anonymous2 [View]

      • MIME types and Servlets that generate binary data
        2004-01-07 18:52:12  jfalkner [View]

  2. MIME types and Servlets that generate binary data
    2003-12-12 13:40:31  anonymous2 [View]

    • MIME types and Servlets that generate binary data
      2003-12-12 14:58:19  jfalkner [View]

      • MIME types and Servlets that generate binary data
        2004-01-26 21:45:24  jleech [View]

1 to 2 of 2