aspose file tools*
The moose likes Servlets and the fly likes setContentType Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "setContentType" Watch "setContentType" New topic
Author

setContentType

Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 311
1) what if i do not write response.setContentType() ?
2) what if i write response.setContentType() at the end of the doGet() method ??
As I understand , the servlet starts flushing its buffer so say i have 20k of data is servlet, 1k will be flushed when available and next and next
now after browser gets 20k data i am sending content type. so how will the browser show it ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
According to the API you must always call setContentType() (if you do call it, that is) before getting a PrintWriter with getWriter().
When I did not do it, I got my HTML rendered as plain text by my browser. Calling setContentType( "text/html" ) enabled the correct rendering.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

It is browser-specific what content type the browser will assume if you do not specify it. So as Barry pointerd out, it's best to be sure and set it so you get correct behavior on the client side.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setContentType
 
Similar Threads
The world will end this Friday, at 11:31:30pm UTC
Is there a solution?
webapp - exporting to excel problem with csv
why should we set response.setContentType before using PrintWriter or OutputStream?
Setting Heap size