2008-03-12
【转】java.lang.IllegalStateException异常产生的原因
http://faq.javaranch.com/view?IllegalStateException
Illegal State Exception
The most common cause of this exception is a servlet or JSP attempting to write to the output stream after the response has been committed.
For servlets, the easiest way to avoid this is to branch the code in your service method in such a way as to insure that calls to HttpServletResponse.sendRedirect or RequestDispatcher.forward are always the last call made before the end of the method. This can be achieved by adding a return call just after either of these.
Example:
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
if("client".equals(request.getParameter("user_type"))){
response.sendRedirect("client-screen.jsp");
return; // <------- This return statement prevents any further writing to the outputStream
}
//
// Other code that may write to the outputStream....
//
}
Another way this can happen is by attempting to redirect from within the middle of a JSP page, eg:
...
<div>hello world</div>
<%
if ("not logged in")
response.sendRedirect("login.jsp");
%>
<div>more stuff</div>
...
Another common place where an IllegalStateException is likely to occur is in a JSP that attempts to stream binary data. JSPs are primarily designed to format output as HTML. With HTML, white space characters are ignored. It is not uncommon for JSP compilers to inject their own white space charaters to the beginning and/or end of the output stream. Line breaks in the developer's code can also be interpreted as white space in the output stream. These white space characters can interfer with the generated servlet's ability to create and stream the binary outputdata, resulting in the IllegalStateException.
The simple solution to this problem is to always us a servlet for streaming binary data.
NOTE: Tomcat 5.5.X seems to be lenient on throwing an IllegalStateException. Although the reponse has been flushed, it seems to allow the redirection, without throwing any exception.
Illegal State Exception
The most common cause of this exception is a servlet or JSP attempting to write to the output stream after the response has been committed.
For servlets, the easiest way to avoid this is to branch the code in your service method in such a way as to insure that calls to HttpServletResponse.sendRedirect or RequestDispatcher.forward are always the last call made before the end of the method. This can be achieved by adding a return call just after either of these.
Example:
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
if("client".equals(request.getParameter("user_type"))){
response.sendRedirect("client-screen.jsp");
return; // <------- This return statement prevents any further writing to the outputStream
}
//
// Other code that may write to the outputStream....
//
}
Another way this can happen is by attempting to redirect from within the middle of a JSP page, eg:
...
<div>hello world</div>
<%
if ("not logged in")
response.sendRedirect("login.jsp");
%>
<div>more stuff</div>
...
Another common place where an IllegalStateException is likely to occur is in a JSP that attempts to stream binary data. JSPs are primarily designed to format output as HTML. With HTML, white space characters are ignored. It is not uncommon for JSP compilers to inject their own white space charaters to the beginning and/or end of the output stream. Line breaks in the developer's code can also be interpreted as white space in the output stream. These white space characters can interfer with the generated servlet's ability to create and stream the binary outputdata, resulting in the IllegalStateException.
The simple solution to this problem is to always us a servlet for streaming binary data.
NOTE: Tomcat 5.5.X seems to be lenient on throwing an IllegalStateException. Although the reponse has been flushed, it seems to allow the redirection, without throwing any exception.
发表评论
- 浏览: 66460 次

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
web ui的显示风格
会成为热门的技术吧
-- by ouspec -
web ui的显示风格
yui-ext很多人在研究……
-- by ouspec -
Java语言的新特性,我们用 ...
Java5: 1。泛型 2。枚举类型 3。自动类型包装和解包装(autob ...
-- by jameswei -
Java语言的新特性,我们用 ...
我是JDK正式版测试的.呵呵 你还真牛,可以让JDKcrash
-- by lbfhappy -
Java语言的新特性,我们用 ...
lbfhappy 写道complystill 写道floating 写道comp ...
-- by 歆渊






评论排行榜