Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not Getting proper output for Servlet Application

 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Actually I'm trying to wright a servlet application in which one servlet(MultipleInlcude.java) tries to include another
servlet(IncludeServlet1.java) which already has other jsp (level3.jsp) included in turn has another servlet (IncludeServlet2.java)
included.

My aim is to display output of all resource on single page.

But here i'm getting output of only IncludeServlet2.java. (Hello from Level4)

Follwing are the codes:

MultipleInclude.java
=============


IncludeServlet1.java
=============


level3.jsp
======


IncludeServlet2.java
=============


web.xml
======



Could anyone please explain me where its going wrong.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't commit to the response (i.e. write to the stream) and then forward it to another resource. Instead you can save the HTML as a request attribute.

 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan,
Thanks for your valuable input.
Could you please pin point where exacly it goes wrong and where the changes need to be made.

I understood your point that you can't commit response and then include(forward) page. But if its the case then i should get an Exception.

But in this case i'm not getting any exception.

Could yoy please explain in brief where the correction are to be made.

Thanks in Advance.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An IllegalStateException is thrown when you call OutputStream#flush before RequestDispatcher#forward, but you may not receive an error page.

See RequestDispatcher#include to accomplish your task.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I had been handed such a complex tangle of interlocking servlets and jsp I would say it is time to back off and redesign the whole thing. I bet there are functions inside servlet code that should be in helper classes that can be tested outside the servlet environment.

Sometimes you just have to throw version 0.1 away and start anew.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic