File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes multiple calls to ServletRequest.getWriter() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "multiple calls to ServletRequest.getWriter() ?" Watch "multiple calls to ServletRequest.getWriter() ?" New topic
Author

multiple calls to ServletRequest.getWriter() ?

Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Hi,

when calling getWriter() following a call to getOutputStream() on a ServletRequest and vice versa results in an IllegalStateException.
1. What happens, if multiple calls are made to either getWriter() or getOutputStream? Same behaviour?
2. In case calling getWriter() multiple times doesn't throw a RuntimeException, are the returned references identical or different?

Kind regards,
Andy


SCJP, SCJD
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1522
    
  22

Hi Andy,

when calling getWriter() following a call to getOutputStream() on a ServletRequest

ok, you mean ServletResponse object (ServletRequest has a getReader() and a getInputStream())

1. What happens, if multiple calls are made to either getWriter() or getOutputStream? Same behaviour?

It will just work, as long as you don't mix them
2. In case calling getWriter() multiple times doesn't throw a RuntimeException, are the returned references identical or different?

They should refer to the same object

Regards,
Frits
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Frits! Why do they fail, if mixed?

Thanks!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
yes, thanks Frits, so I conclude that the following statement always evaluates to true:

and the same is with getInputStream(), getWriter(), getOutputStream()
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1522
    
  22

Hi Abimaran,
Why do they fail, if mixed?

It has to do with the characteristics of the underlying stream:
  • Input/Output Stream classes are used for binary data
  • Reader/Writer classes are used for character based data


  • You either read/write binary data or characters.

    Regards,
    Frits
    Abimaran Kugathasan
    Ranch Hand

    Joined: Nov 04, 2009
    Posts: 2066

    Thanks a lot Frits! Thanks for the help!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: multiple calls to ServletRequest.getWriter() ?
     
    Similar Threads
    File is Not Displaying
    Good question
    getWriter()
    setContentType and getOutputStream
    setContentType