• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setcontenttype()- How many times can we call?

 
Sirisha Reddy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a single response object can we call setcontenttype() method multiple times?
Example situation: Say i need to send an image and a .jar file using the same response object.
 
Sirisha Reddy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No answer or is it just a stupid question
 
william shen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can call it multi-times, but only the first call will be effective. You might use the outputstream in your case.
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"public void setContentType(java.lang.String type)
Sets the content type of the response being sent to the client. The content type may include the type of character encoding used, for example, text/html;charset=ISO-8859-4.
If obtaining a PrintWriter, this method should be called first."
This means that, you have to invoke setContentType before obtaining a PrintWriter. The spec doesn't specify what happens if you "break the rule" and invoke setContentType after obtaining a PrintWriter, but typically what servlet containers do is ignore the subsequent calls. But beware: You may call setContent as many times as you like before obtaining a PrintWriter. So, for instance in the following code snippet:
response.setContentType("text/html;charset=ISO-8859-1");
response.setContentType("text/html;charset=ISO-8859-7");
PrintWriter = response.getWriter();
response.setContentType("text/html;charset=ISO-8859-8");
the Greek ISO charset (ISO-8859-7) will get used replacing the Latin-1 one (ISO-8859-1). Setting to the Hebrew ISO charset (ISO-8859-8) is illegal as the PrintWriter object has already been obtained, and will typically get ignored.
 
Eddie Sheffield
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If my understanding is correct, I have to admit I can't quite think of a real situation where you would need to do this. What kind of client are you replying to? If it's standard web browser then you should never be sending more than one kind of data in a response at a time. For example loading a web page containing an image and a jar file (applet) the browser will send 3 separate requests - one for the enclosing HTML, one for the image, and one for the JAR. Assuming your servlet is configured to handle all these access, then it will have to examine the request to see what is being requested, set the content type and return the appropriate data.
On the other hand if you are actually communicating with some kind of client that can handle receiving multiple types in a single stream (something like a MIME message perhaps) then it will probably have it's own specific protocol you'll have to follow anyway. For such a beast I would expect it might use some kind of custom content type setting and would have it's own way of identifying the different parts of the data stream.
I may well be misunderstanding something here though, so don't hesitate to say so!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic