Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Content type / MIME mapping question

 
Yong Chen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Sybex book <<Java 2 Web Developer Certification Study Guide>> :
---------------------------------------------
When a servlet sends a response to a client, the browser needs to know how to render the information received. Consequently, the server can construct a response to notify the client of the MIME type, by using two different approaches:
. By using the HttpServletResponse�s method: setContentType(�)
. By using the mime-mapping tag in the web.xml file for the web application
---------------------------------------------
Does it means I could choose either approach?
If I won't call setContentType(...), how could the browser know the content type?
If I have to call setContentType(...), what is mime-mapping for?
Thanks.
[ July 22, 2003: Message edited by: Yong Chen ]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can know the Mime-type from the extention of the resource requested by the url.

Now, if the url is http://localhost:8080/catalog/page1.pdf The request is going to be serviced by the catalog servlet. Since the extention on the resource is pdf. The Servlet contain knows to set the Mime-type to application/pdf What is cool, if you add a mapping for xml, a simple change of the url to http://localhost:8080/catalog/page1.xml can be rcognized and if you look up the file in your servlet, you don't have to do any lookup to have the content type set properly.
 
Yong Chen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still confused.
Do you mean, with url "http://localhost:8080/catalog/page1.pdf", the Servlet container will set the MIME type as "application/pdf" automatically?
If the servlet "catalog" just send a "Hello World" as response, whatever the PathInfo is (page1.pdf or page1.xml), the client browser shows "Hello World".
Even if I set the content type manually:
-------------------------------
res.setContentType("application/pdf");
// or: res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("<http><body><p>Hello world</body></html>");
out.flush();
-------------------------------
the browser treats it as "text/html".
How to explain this?
Thanks,
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the mime-mapping tag only to replace the setContentType method? Or does it have any other significance ?

Thanks
Devi
 
Radmika Arunachalam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Which one takes precedence? if i said both mime-mapping and setContentType different, what is the result.. for example

URL is http://localhost:8080/test/test.pdf

mime-mapping is
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>

and i mapped pdf extension to one servlet and in the servlet i am calling setContentType("text/plain");

What is the actual contenttype of the response sent to browser?

Thanks
Radmika
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic