Meaningless Drivel is fun!
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes pageContext.forward Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "pageContext.forward" Watch "pageContext.forward" New topic


Dominic Paquette
Ranch Hand

Joined: Dec 13, 2002
Posts: 64
Hi everybody, this is my first post on this forum, but, I read it alot while studying for my scjp exam and I found it really usefull. anyway here is my question:
I am trying to forward a user to a html page, for that I use PageContext.forward in my jsp page and Tomcat throws a IllegalStateException. here is the jsp page:
<%@ page language="java" %>
<% pageContext.forward("/html/test.html"); %>
From what I understand (not much!) is that the exception is thrown because the response buffer is flushed. So why is the buffer flushed? I haven't written to it. Althought in the generated java file, I can see that the directive :<%@ page language="java" %> causes a JspWriter.write call!
And if I remove the directive the forward call works fine. If anyone knows what I'm doing wrong...
Thanks in advance
Rasika Chitnis
Ranch Hand

Joined: Jun 19, 2001
Posts: 131
I wonder if setting buffer to some value e.g. 64kb in the page directive will make any difference.
Vidya Nayak

Joined: Apr 20, 2002
Posts: 24
As per Manning Book....
If the output is buffered, it is first cleared and then the request is forwarded to the other resources. If the output is not buffered and /or if the response is already committed by the forwarding resource, then a java.lang.illegalStateException is raised when we attempt to forward the request.
Maha Annadurai
Ranch Hand

Joined: Oct 27, 2002
Posts: 87
If there is any new lines before and after your code , the converted class tries to write to JspWriter. Like out.write("\r\n"); According to spec, if the output is buffered , it must be cleared before forwarding. So just writing few lines to JspWriter is not a problem since it will be cleared. But make sure you do not have any newlines after your forward call .
Also note that, the default value for "autoFlush" is true for <%@ page directive, which means if the written content is greater than the buffer size, then the output will be automatically commited. Also once a request is forwarded to another resource, it is said to be committed, so we should not try to write to JspWriter after that.
So please try the following
1. Make sure there is no space / new lines after the forward call.
2. Try not to write anything before the forward call.
3. you can just write out.clear() to make sure the buffer is cleared , before the forward call.

Maha Anna
[ December 15, 2002: Message edited by: Maha Annadurai ]
I agree. Here's the link:
subject: pageContext.forward
It's not a secret anymore!