• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to the output after forward

 
Biren shah
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
i need help regarding some lines of code..

...
...(lines of code)
out.println("hi");
requestDispatcher view = request.getRequestDispatcher("biren.jsp");
view.forward(req,res);
out.println("biren");
...
...(more lines of code ending the servlet)

As far as i know..if the response is committed then we can not use the requestdispatcher to forward the request.

but if i have not commited the response with flush() method...and i m forwarding to a jsp or servlet ..then whether the output would contain my name biren?>>>
pleae help me out...

if the biren.jsp does not have any code that can be output..what will be the output.
 
Adam Czysciak
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
During my tests I had an exception when I printed something after forward (just because I forgot to add "return" after forwarding to my custom error page instead of desired View jsp).
 
Biren shah
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,.

i need some more explanations like..
if exception is there..than what kind of exceptions..
i dont think we need to write and return statement..
if we need to write..a return statement then ..what is the reasone for writing it.???

please forgive me for my typos
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

but if i have not commited the response with flush() method...and i m forwarding to a jsp or servlet ..then whether the output would contain my name biren?>>>
pleae help me out...

if the biren.jsp does not have any code that can be output..what will be the output.


when the forward method return to the calling servlet, at this stage the response is in commited state. After the calling of forward method, you can do the other processing , but it is not recommended so we put return immediately after it.

As the response is already commited (invalid) you can not view any contains in the browser generated after the forward method. If you use the out.println statement after forward method and the response is not closed , it throws the IllegalStateException, but it can not displayed in browser. You can check your log file.

If ypu put the statement.

log(response.isCommited()); after the forward statement, it shows tye value true.

Hope it help.

Thanks
 
Biren shah
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks narendra and all others..
i got it now..
thanks for the help
 
Cosmin Nicolae Vacaroiu
Ranch Hand
Posts: 55
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems logic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic