• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getOutputStream issue

 
Chandrakanth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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();
%>
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
response.getOutPutStream();

response.getOutputStream();
[ June 05, 2006: Message edited by: wise owen ]
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • 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.
 
joko mujoko
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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();
}
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic