• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help please: forwarding a request in a jsp

 
Jim So
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, If you forward a request in a jsp to another page, then can you still call out.println()? Or will you get a runtime error?

Also if you do

out.println("1");
RequestDispatcher view = request.getRequestDispatcher("secondJSP.jsp");
view.forward(request,response);
out.println("2");


======
second.jsp:

out.println("in second jsp");


What will be outputted to the client?

a) 1 in second jsp 2
b) 1
c) 2
d) in second jsp
e) runtime error

and if you get a runtime error, then at which point is is thrown?

If i didnt have the line out.println("2"); would it still output "1 in second jsp"?
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on my knowledge, I feel you get IllegalStateException as the reponse is already flushed by the time you dispatch it.

Cheers,
Naren

SCJP - 93%
SCDJWS - 86%
SCWCD - ?
 
vani venkat
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jim
i tried this example and got
is in second jsp
as output. however if you add out.flush(); in first jsp before getting request dispatcher and forward, only the contents written before forward happens are shown as output.

if out.flush is called abd then if you forward to another jsp, contents written in first jsp are shown as output and nothing after forward shows up on page.
however if you do forward (like in this example), response is cleared out and only the contents of second jsp are shown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic