• 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

getOutputStream issue

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

can't we do this in JSP,
i am getting the error The method getOutPutStream() is undefined for the type HttpServletResponse

<%
response.getOutPutStream();
%>
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
response.getOutPutStream();

response.getOutputStream();
[ June 05, 2006: Message edited by: wise owen ]
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, even if you do use the correct method name, you'll find you get an IllegalStateException. The reason is that JSPs use a JspWriter behind the scenes, so in fact the getWriter method was already invoked. By the contract of ServletResponse, it is illegal to invoke both getOutputStream() and getWriter().

Why would you want to invoke both anyway; the mere fact you're using a JSP implies that you are writing a textual (e.g. HTML, XML) response. The output stream should be used for binary responses.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

can't we do this in JSP,
i am getting the error The method getOutPutStream() is undefined for the type HttpServletResponse

<%
response.getOutPutStream();
%>



I agree with wisw owen

response.getOutPutStream();

response.getOutputStream();
[ June 05, 2006: Message edited by: wise owen ]



because the error is"method is undefined"


In fact, even if you do use the correct method name, you'll find you get an IllegalStateException. The reason is that JSPs use a JspWriter behind the scenes, so in fact the getWriter method was already invoked. By the contract of ServletResponse, it is illegal to invoke both getOutputStream() and getWriter().

Why would you want to invoke both anyway; the mere fact you're using a JSP implies that you are writing a textual (e.g. HTML, XML) response. The output stream should be used for binary responses.



in case that we need to render image for jsp just need to set
<%response.setContentType="image/jpeg";%>

and assign your getOutputStream to a variable, all is work and no illegal

the snippet is

byte[] b = null;
b = (byte[])o;
if(b != null)
{
response.setContentType("image/jpeg");
OutputStream ox = response.getOutputStream();
ox.write(b);
ox.close();
}
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joko,

in case that we need to render image for jsp just need to set



You meant "servlet", not JSP, right?

This is incorrect :


I guess you meant



But it would have no effect because ServletResponse.setContentType(...) does not set the response's character encoding if it is called after getWriter().

"response.getOutputStream();" would generate a IllegalStateException for the same reason, see Charles' post above.

JSP is meant to generate text pages, most of the time HTML, they are not meant to send binary data. You'd rather user a servlet to serve binary data.
 
joko mujoko
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for correcting the code Rodrigo Alvarez,

well what I did is, I use this code in one file of jsp as part of another page (as tiles in struts). And no illegalStateException beacuse we use in the other translation and compilation.

Joko
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic