• Post Reply Bookmark Topic Watch Topic
  • New Topic

Illegal State Exception: Ref Page 412 HF Book

 
AmitKumar Jain
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is in reference to page 412 of HF book which says that an Illegal State Exception is thrown when the output is flushed before a forward action.

I tried a similar example in tomcat5 but it does not throw any exception. It simply does not forward to the page.

CASE1:
****************************************************
<html>
<body>
Hello!!
<%
out.println("Before forward/include");
out.flush();
%>
<jsp:forward page="new.jsp" />
<%
out.println("After forward/include");
%>
</body>
</html>

output:
Hello!! Before forward/include
***********************************************

CASE2:
Now when I comment "out.flush();"
****************************************************
<html>
<body>
Hello!!
<%
out.println("Before forward/include");
//out.flush();
%>
<jsp:forward page="new.jsp" />
<%
out.println("After forward/include");
%>
</body>
</html>

Contents of new.jsp:
<html>
<body>
Included File
</body>
</html>

output:
Included File
********************************************************

CASE 3:
Problem with <jsp:include > action.
Normally I believed that jsp include threw an exception if the included
file does not exist. But I'm surprised to see that it simply skips if file doesnt exist.

<html>
<body>
Hello!!
<%
out.println("Before forward/include");
%>
<jsp:include page="new1.jsp" />
<%
out.println("After forward/include");
%>
</body>
</html>

output:
Hello!! Before forward/include After forward/include

Note: new1.jsp does not exist.

****************************************************
All examples were tested on tomcat5.

Regards,
Amit.
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit,

I think this is the reason behind your first result :

the forward does throw an IllegalStateException, but as this is JSP, which is later converted into a Servlet, this whole thing goes into a try/catch block in _jspService method, where this exception is caught, but no action is taken.
You should see the servlet code generated for this jsp, then you would understand...

For the second result : you have commented the out.flush() before forward.
Whenever a forward happens, the buffer is cleared and the output of the forward file is stuffed into the response and sent to the client. but when you try to write to the response after that it throws an IllegalStateException, which is caught by the try/catch block of _jspService() method.

for the third result, I am not sure why it doesnot throw an Exception.

Regards,

Sushma
 
Catherine van Borselaer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

For the first example, you can see that the IllegalStateException is thrown. Look for it in the log. (directory tomcat/logs) It is not on the screen because with the flush() method you simply but all thats in the buffer to the screen. After that you try to do stuff, but it is too late.

For the third example, are you sure the compiled jsp is also not available? If you delete your jsp file from your webapp, you also checked if it is not in the work directory? Look for the .class file over there. Probably it is called new1_jsp.java and new1_jsp.class.

Good luck!
[ June 21, 2005: Message edited by: Catherine van Borselaer ]
 
AmitKumar Jain
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sushma & catherine!!

I found the exceptions in the logs!!


1)StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: Error: Attempt to clear a buffer that's already been flushed

2)Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /badPage.jsp(9,0) File "/new1.jsp" not found

Now I believe that in 1'st case, the exception didn't appear on screen due to out.flush(), but I didn't use flush in 3'rd example. Then how come the exception didn't appear on screen but went to logs!!!
 
AmitKumar Jain
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry folks!
I wrote the second point wrongly in my last post...

"Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /badPage.jsp(9,0) File "/new1.jsp" not found"
occurs at compile time with include directive.


I verified the results of 3'rd case again today, and found that in case of <jsp:include action, the file gets compiled and no exception is thrown at compile time even if the included file does not exist ( this is obvious ,as jsp action occurs at runtime)

Here is the generated servlet code:
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class badPage_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {

private static java.util.Vector _jspx_dependants;

public java.util.List getDependants() {
return _jspx_dependants;
}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;


try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;

out.write("<html>\r\n");
out.write("<body>\r\n");
out.write("Hello!!\r\n");

out.println("Before forward/include");

out.write('\r');
out.write('\n');
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "new1.jsp", out, false);
out.write('\r');
out.write('\n');

out.println("After forward/include");

out.write("\r\n");
out.write("Good Bye!\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}

Now I don't think that any runtime exception is thrown as
out.println("After forward/include");
out.write("\r\n");
out.write("Good Bye!\r\n");
get executed after call to JspRuntimeLibrary.include()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!