This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pageContext & RequestDispatcher

 
Gaurav Gambhir
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pageContext.include() works fine for both HTML and JSP pages
but pageContext.forward() works fine only for JSP pages not for HTML pages why?

i.e we have this pageContext.jsp file,consider pageContext.jsp and test.html have public access, outside WEB-INF.
<html>
<body>
<%pageContext.forward("test.html");%>
</body>
</html>
gives java.lang.IllegalStateException: getOutputStream() has already been called for this response

while

<html>
<body>
<%pageContext.forward("test.jsp");%>
</body>
</html>

Works fine and forward it to jsp page Why?

-----------------------------------------------


and why RequestDispatcher doesnot work with html pages,for both forward and include?
i.e for this particular case
<html>
<body>
Hello
<% RequestDispatcher view =request.getRequestDispatcher("test.html");
view.forward(request,response);
%>
</body>
</html>
java.lang.IllegalStateException: getOutputStream() has already been called for this response
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a snippet of the source code Tomcat generates for your pageContext.jsp.
Notice anything?

All template text and HTML tags are placed in out.write() statements.
Which were then called after the forward was done.

To correct this either remove all text after the forward in the jsp.
Including html tags and carriage returns.

Or, set some variable and forward based on its value. This way
the out of the HTML after the forward can be controlled.
Example:
 
Gaurav Gambhir
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EdWard, Thanks but I still have a doubt

All template text and HTML tags are placed in out.write() statements.
Which were then called after the forward was done.


Above statement is true in both cases i.e if we forward to HTML or JSP page, but if we forward to JSP page it works fine,Why so?
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this is a little speculative but I wonder if it has to do with the fact of forwarding to a static resource(test.html) as opposed to a dynamic resource(test.jsp). When the container does the forward to test.jsp, test.jsp actually takes control and the JspWriter of the forwarding page falls out of scope (I guess its thread dies) while the forwarded to page starts in a new thread and creates a new pageContext and JspWriter. Since this is not done for a static resource (as far as I can tell its not), the thread the initial jsp is executing in continues to run and since the buffer was flushed with the call to forward(), the IllegalStateException is thrown.

Again, pure speculation on my part.
[ June 16, 2006: Message edited by: Ed Ward ]
 
Gaurav Gambhir
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ed Ward,seems to be the right solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic