aspose file tools*
The moose likes Tomcat and the fly likes Getting IllegalStateException from catalina Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Getting IllegalStateException from catalina" Watch "Getting IllegalStateException from catalina" New topic
Author

Getting IllegalStateException from catalina

Monikaa Agassi
Greenhorn

Joined: Jan 30, 2006
Posts: 5
Hi,
I am getting following exception after login :

[ERROR,Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:750)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:165)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:166)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:158)
at org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:205)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:177)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:198)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:193)
at org.apache.jsp.companyImageServer$jsp._jspService(companyImageServer$jsp.java:493)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
at java.lang.Thread.run(Thread.java:534)
[ERROR,Engine] ErrorDispatcherValve[localhost]: Exception Processing ErrorPage[exceptionType=java.lang.Throwable, location=/jsp/error.jsp]
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.reset(ResponseFacade.java:243)
at org.apache.catalina.valves.ErrorDispatcherValve.custom(ErrorDispatcherValve.java:384)
at org.apache.catalina.valves.ErrorDispatcherValve.throwable(ErrorDispatcherValve.java:250)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:178)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
at java.lang.Thread.run(Thread.java:534)



Following is the code snippet for companyImageServer.jsp:
\\\
<%@ page import="java.util.*,java.net.*,java.rmi.*,javax.naming.*,javax.rmi.*,javax.ejb.*,
com.zeborg.cubem.model.CubeMeta,
com.zeborg.cubem.model.ModelUtil,
com.zeborg.expensemap.model3.DataSet"
%><%@ page import="com.zeborg.util.*,com.zeborg.ejb.*,java.io.*"
%><%@ page import="com.zeborg.expensemap.view.*,com.zeborg.expensemap.view2.*"
%><%@ include file="begin_page.jsp"
%><%
// This page will stream the company logo to the calling jsp page
//No client side caching
WebUtil.addNoCache(response);

String cubekey = request.getParameter("key");
String location = request.getParameter("location");
if (cubekey==null)
{
throw new RuntimeException("Could not determine selected cube.");
}

CubeMeta cube = com.zeborg.cubem.model.ModelUtil.getCubeMetaHome().findByPrimaryKey(cubekey);
DataSet ds = EMZbaseUtil.getDataSet(cube);
if (cube==null)
throw new RuntimeException("Could not find cube in model:"+cubekey);

String imagename="";
if (location != null && location.equalsIgnoreCase("masthead"))
imagename=ds.getACCompany().getImageFileName();
else
imagename=ds.getACCompany().getMastheadImageFileName();

if(imagename!=null)
{
String imageext=imagename.substring(imagename.lastIndexOf("."));
if(imageext.equalsIgnoreCase("jpg") || imageext.equalsIgnoreCase("jpeg") )
response.setContentType("image/jpg");
} else {
response.setContentType("image/gif");
}
response.setHeader("Content-Disposition", "filename="+imagename);
OutputStream os = pageContext.getResponse().getOutputStream();
os.flush();
CubeUtil.streamCompanyImage(os,location,cubekey);
os.flush();
os.close();
// throw new com.zeborg.web.FinishPageException();

%><%@ include file="end_page.jsp"%>
\\\\

I Am using JBoss-2.4.11 with Tomcat-4.0.6
Does anybody know , why this can happen ?
Thanks in advance.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This is precicely why you should use a servlet for streaming binary data and not a JSP.
What's happening is that the JSP engine is trying to add whitespace to the output stream after you've already closed it.

You may be able to get it to work by:
Getting rid of the include at the bottom of the page,
adding a return statement just after closing the outputStream,
and making sure that there is absolutely no whitespace between any of your JSP tags.

Of course, this will make your JSP quite ugly and un-readable.
It's a lot easier (in my opinion) just to move this functionality into a servlet and leave JSP to do what it does best; stream text to browsers.
[ January 30, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Monikaa Agassi
Greenhorn

Joined: Jan 30, 2006
Posts: 5
Is there any generic solution to this problem, as there can be many instances like this in the application ?

Thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
The generic solution is to use JSP for character output and servlets for binary output as intended by the designers and generally accepted in the servlet/jsp community. Since the browser constructs a page from multiple requests, there really should be no problem directing img requests to the appropriate servet.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting IllegalStateException from catalina