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 OutputStream  in Jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "OutputStream  in Jsp" Watch "OutputStream  in Jsp" New topic
Author

OutputStream in Jsp

Abir Majumder
Greenhorn

Joined: Dec 06, 2008
Posts: 3
<%
OutputStream os = response.getOutputStream();
os.print("test".getBytes());
%>
a. The code will print the ascii values of the string 'test' to the Browser.
b. The program won't compile because of compilation errors.
c. The program will throw a run-time IllegalStateException.
d. The code will print 'test' in the Browser window.

I think the ans is b - OutputStream has no method as print.
But the ans is c.
The code will throw a Run-time exception, as it is not possible to call both the getWriter()
and getOutputStream() method on the same response object. Remember that for a JSP
page, the method getWriter() would have been called already by the Container and will be
stored in the 'out' variable.

Please comment.
Thanks
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
You are better off using a Servlet rather than a JSP here since "out" is already set by the JSP compiler by getting a Writer from the response stream.


SCJP 1.4 85%
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Yes thats correct.

Within a JSP you cannot invoke getWriter() and getOutputStream().

getWriter() javadoc:
Either this method or getOutputStream() may be called to write the body, not both.
java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object

getOutputStream: javadoc:
Either this method or getWriter() may be called to write the body, not both.java.lang.IllegalStateException - if the getWriter method has been called on this response


Hence one thing is clear that either one of them must be called and not both.

Now when you write a JSP , JSP has a implicit object named out which gets intialized as
PrintWriter out = response.getWriter();

Now in your JSP if you have
<%
OutputStream os = response.getOutputStream();
os.print("test".getBytes());
%>

Have a look at the _jspService() of the generated servlet and you will see

PrintWriter out = response.getWriter();
OutputStream os = response.getOutputStream();

Hence you will see an exception IllegalStateException thrown by getOutputStream()


SCJP, SCWCD, SCBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OutputStream in Jsp
 
Similar Threads
solutions to multiple q, i attentd a test and want to coform the solutions
To Nikhil Pendharkar for IBM 287
variable this is undefined ?
jsp /java i m not able to fine currect solutions
more john hunt questions