• 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

Doubt in servlet regarding getWriter() and output stream.

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. When we will use addHeader method of response object?

2. Why servlet container is throwing IllegalStateException when am using both getWriter() and getOutputStream? Why can't i use both?

3. In a servlet am using res.getOutputStream() to write anything in the browser instead of PrintWriter.
In the same servlet am including one jsp. In the jsp i am using a implicit object 'out' to print something. The container throws illegal state exception in this scenario. If i am replacing the res.getOutputStream() with res.getWriter() in the servlet, it is not throwing any exception. What could be the reason?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sriram,

I'm just starting to sudy for SCWCD, but anyway I'll try to answer your questions. I hope someone will read and correct if I'm wrong.

1: We use setHeader when defining the value of some HTTP header attribute. When some HTTP attribute allows multiple values (unfortunatelly I don't know an example of this) you should use addHeader to add the additional values.

2,3: The IllegalStateException exception is supposed to be thrown when you try to use both the PrinterWriter and the OutputStream objects. PrinterWriter is recommended to be used for text output, and OutputStream to generate binary output. I know that someone has a better and complete explanation for the reasons why one object should be used for text and the other for binary, but this is what I read in a online tutorial.
Maybe this is enough to answer the certification questions

Regards,

Alexandre.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. One example of using headers is special last modified header. Browser can read the value of the header, check it with its cache and do not download any contents, if it already has current version of the resource
2. This behaviour is defined in specification. Simply put, writer is for text, OutputStream - for binary data.
3. Jsp you include (and all jsps in general) uses writer to writeOut its result. And if your servlet uses OutputStream you cannot switch to Writer - see p. 2.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic