Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a simple question

 
Ravishanker kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be output on page and server when test1.jsp will be requested?Why?

/*test1.jsp*/
<%
1. RequestDispatcher rd=request.getRequestDispatcher("/test2.jsp");
2. rd.forward(request,response);
3. out.println("After forward");
4. System.out.println("After forward");
%>


/*test2.jsp*/
<%
out.println("Test 2");
%>

I tried this with tomcat 5.5.
And answer is: �Test2� is printed on the page and �After forward� on server.
But I am confused If line 4 is executed then why there is no output for line 3?
Can anyone explain it?
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

After the call retun from the requestDispatcher forward method to the calling page the response is invalid. That means you can not write anything to response. But any other codes executed perfectly, but not recommended to do anything after this call in calling page.

Thanks
 
Ravishanker kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.

I have some more queries regarding this.

Is the implementation of out.println() is server dependent?
Is there any possibility that it may throw IlllegalStateException because we are trying to send the response after committing?
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't think so. Before the forward method returns,the response content is sent and committed, and closed by the container. So no effect on calling the method on the closed writer.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic