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

Doubt in the write method

 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the below code we have used PrintWriter instance variable and called print,println and write methods on it
<blockquote>code:
<pre name="code" class="core">
PrintWriter out=response.getWriter();
out.print("calling print on printwriter");
out.println("calling println on printwriter");
out.write("calling write on printwriter");
</pre>
</blockquote>

all of them are working in the same way that is the text is print on the screen when i make a call to the servlet but in the below code
<blockquote>code:
<pre name="code" class="core">
ServletOutputStream sout=response.getOutputStream();
sout.print("calling print on ServletOutputStream");
sout.println("calling println on ServletOutputStream");
// sout.write(1);
</pre>
</blockquote>

when i comment the write() then it works in the same way as above it prints the text on the screen but when enable the write() and invoke the url instead of displaying the text on the screen it gives file to download which will contain the text.

why write() is behaving differently in this two context?

and one more thing when i have only one snippet of the code in a servlet then above sed behaviour will take place but when i both of them at a time it will throw a "java.lang.IllegalStateException: getWriter() has already been called for this response"Exception. why this problem occurs?

Thanks
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and one more thing when i have only one snippet of the code in a servlet then above sed behaviour will take place but when i both of them at a time it will throw a "java.lang.IllegalStateException: getWriter() has already been called for this response"Exception. why this problem occurs?


This problem or exception occurs because you cannot have more than one writers for a single response, also it is not logical to do so unless you are doing it for curiosity.


why write() is behaving differently in this two context?

Not sure but maybe it is due to the buffering that takes place or maybe they just behave differently.


Hope this helps
 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply,any updates on this?

Thanks
 
Amit Prakash Singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raja Ram,

ServletOutputStream extends java.io.OutputStream. We have 3 write menthods in OutputStream. write(byte[] b) , write(byte[] b, int off, int len) and write(int b). Out of these 3 only 2 [write(byte[] b) , write(byte[] b, int off, int len)] have been implemented in the OutputStream class and the 3rd one has been left for the implementation.

"Subclasses of OutputStream must provide an implementation for this method. "

Now : This is an abstract class that the servlet container implements. Subclasses of this class must implement the java.io.OutputStream.write(int) method.

So the behaviour is container specific.

"Also it Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored."

All the comments in "" taken from sun doc on the APIs
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic