• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help please: forwarding a request in a jsp

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 - ?
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
no wonder he is so sad, he hasn't seen this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic