my dog learned polymorphism
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Help please: forwarding a request in a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Help please: forwarding a request in a jsp" Watch "Help please: forwarding a request in a jsp" New topic

Help please: forwarding a request in a jsp

Jim So

Joined: Mar 18, 2009
Posts: 28
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

RequestDispatcher view = request.getRequestDispatcher("secondJSP.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

Joined: Feb 03, 2004
Posts: 577

Based on my knowledge, I feel you get IllegalStateException as the reponse is already flushed by the time you dispatch it.


SCJP - 93%
SCDJWS - 86%

(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
vani venkat
Ranch Hand

Joined: Nov 21, 2006
Posts: 142
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.

SCJP 1.4, SCWCD 1.5
I agree. Here's the link:
subject: Help please: forwarding a request in a jsp
It's not a secret anymore!