• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Content type / MIME mapping question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic